Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions contracts/cw721-proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "cw721-proxy"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

[features]
# enable feature if you want to disable entry points
library = []

[dependencies]
cosmwasm-schema = "^1.5"
cosmwasm-std = "^1.5"
cw2 = "^1.1"
serde = { workspace = true }
cw721 = {git = "https://github.com/public-awesome/cw-nfts.git", version = "0.19.0"}
cw-ownable = "2.1.0"
cw-storage-plus = "^1.1"
thiserror = "1.0.64"
41 changes: 41 additions & 0 deletions contracts/cw721-proxy/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::DefaultCw721ProxyContract;
use crate::{ContractError, CONTRACT_NAME, CONTRACT_VERSION};
use cosmwasm_std::entry_point;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response};
pub use cw721::*;
pub use cw_ownable::{Action, Ownership, OwnershipError};

#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
let contract = DefaultCw721ProxyContract::default();
contract.instantiate_with_version(deps, &env, &info, msg, CONTRACT_NAME, CONTRACT_VERSION)
}

#[entry_point]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
let contract = DefaultCw721ProxyContract::default();
contract.execute(deps, env, info, msg)
}

#[entry_point]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
let contract = DefaultCw721ProxyContract::default();
contract.query(deps, env, msg)
}

#[entry_point]
pub fn migrate(deps: DepsMut, env: Env, msg: MigrateMsg) -> Result<Response, ContractError> {
let contract = DefaultCw721ProxyContract::default();
contract.migrate(deps, env, msg, CONTRACT_NAME, CONTRACT_VERSION)
}
21 changes: 21 additions & 0 deletions contracts/cw721-proxy/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[derive(Debug, thiserror::Error)]
pub enum ContractError {
#[error(transparent)]
Std(#[from] cosmwasm_std::StdError),

#[error(transparent)]
Cw721(#[from] cw721::error::Cw721ContractError),

// #[error(transparent)]
// Encode(#[from] cosmos_sdk_proto::prost::EncodeError),
//
// #[error(transparent)]
// Decode(#[from] cosmos_sdk_proto::prost::DecodeError),
#[error("unauthorized")]
Unauthorized,

#[error("invalid_msg_type")]
InvalidMsgType,
}

pub type ContractResult<T> = Result<T, ContractError>;

Check warning on line 21 in contracts/cw721-proxy/src/error.rs

View workflow job for this annotation

GitHub Actions / Test Suite

type alias `ContractResult` is never used
77 changes: 77 additions & 0 deletions contracts/cw721-proxy/src/execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::msg::{get_inner, ExecuteMsg, InnerExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
Copy link
Contributor

@adairrr adairrr Nov 15, 2024

Choose a reason for hiding this comment

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

This cw721-proxy acts as an cw721 adapter module within Abstract SDK: https://docs.abstract.money/3_framework/6_module_types.html#adapters

Happy to go over the different modules and how to build them, as it removes a ton of boilerplate, makes the contract more upgradeable, and the module code has already been audited.

use crate::state::DefaultCw721ProxyContract;
use crate::ContractError;
use crate::ContractError::Unauthorized;
use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response};
use cw721::traits::{Cw721Execute, Cw721Query};

impl DefaultCw721ProxyContract<'static> {
pub fn instantiate_with_version(
&self,
deps: DepsMut,
env: &Env,
info: &MessageInfo,
msg: InstantiateMsg,
contract_name: &str,
contract_version: &str,
) -> Result<Response<Empty>, ContractError> {
// set the proxy addr
self.proxy_addr.save(deps.storage, &msg.proxy_addr)?;

// passthrough the rest
Ok(self.base_contract.instantiate_with_version(
deps,
env,
info,
msg.inner_msg,
contract_name,
contract_version,
)?)
}

pub fn execute(
&self,
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response<Empty>, ContractError> {
match msg {
ExecuteMsg::UpdateExtension { msg: proxy_msg } => {
let proxy_addr = self.proxy_addr.load(deps.storage)?;
if info.sender.ne(&proxy_addr) {
return Err(Unauthorized);
}

let new_info = MessageInfo {
sender: proxy_msg.sender,
funds: info.clone().funds,
};
Ok(self
.base_contract
.execute(deps, &env, &new_info, proxy_msg.msg)?)
}
_ => {
let inner_msg: InnerExecuteMsg = get_inner(msg)?;
Ok(self.base_contract.execute(deps, &env, &info, inner_msg)?)
}
}
}

pub fn query(&self, deps: Deps, env: Env, msg: QueryMsg) -> Result<Binary, ContractError> {
Ok(self.base_contract.query(deps, &env, msg)?)
}

pub fn migrate(
&self,
deps: DepsMut,
env: Env,
msg: MigrateMsg,
contract_name: &str,
contract_version: &str,
) -> Result<Response, ContractError> {
Ok(self
.base_contract
.migrate(deps, env, msg, contract_name, contract_version)?)
}
}
11 changes: 11 additions & 0 deletions contracts/cw721-proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[cfg(not(feature = "library"))]
pub mod contract;
mod error;
mod execute;
pub mod msg;
mod state;

pub use crate::error::ContractError;

pub const CONTRACT_NAME: &str = "cw721-proxy";
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
103 changes: 103 additions & 0 deletions contracts/cw721-proxy/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Addr, Empty};

use crate::ContractError;
use crate::ContractError::InvalidMsgType;
use cw721::{
msg::{Cw721ExecuteMsg, Cw721InstantiateMsg, Cw721MigrateMsg, Cw721QueryMsg},
DefaultOptionalCollectionExtension, DefaultOptionalCollectionExtensionMsg,
EmptyOptionalNftExtension, EmptyOptionalNftExtensionMsg,
};

#[cw_serde]
pub struct InstantiateMsg {
pub proxy_addr: Addr,

pub inner_msg: Cw721InstantiateMsg<DefaultOptionalCollectionExtensionMsg>,
}

pub type ExecuteMsg =
Cw721ExecuteMsg<EmptyOptionalNftExtensionMsg, DefaultOptionalCollectionExtensionMsg, ProxyMsg>;
// pub type InstantiateMsg = Cw721InstantiateMsg<DefaultOptionalCollectionExtensionMsg>;
pub type MigrateMsg = Cw721MigrateMsg;
pub type QueryMsg =
Cw721QueryMsg<EmptyOptionalNftExtension, DefaultOptionalCollectionExtension, Empty>;

pub type InnerExecuteMsg =
Cw721ExecuteMsg<EmptyOptionalNftExtensionMsg, DefaultOptionalCollectionExtensionMsg, Empty>;

#[cw_serde]
pub struct ProxyMsg {
pub sender: Addr,
pub msg: InnerExecuteMsg,
}

pub fn get_inner(msg: ExecuteMsg) -> Result<InnerExecuteMsg, ContractError> {
match msg {
ExecuteMsg::UpdateOwnership(_0) => Ok(InnerExecuteMsg::UpdateCreatorOwnership(_0)),

Check warning on line 37 in contracts/cw721-proxy/src/msg.rs

View workflow job for this annotation

GitHub Actions / Test Suite

use of deprecated tuple variant `cw721::msg::Cw721ExecuteMsg::UpdateOwnership`: Please use UpdateMinterOwnership instead

Check warning on line 37 in contracts/cw721-proxy/src/msg.rs

View workflow job for this annotation

GitHub Actions / Test Suite

use of deprecated field `cw721::msg::Cw721ExecuteMsg::UpdateOwnership::0`: Please use UpdateMinterOwnership instead
ExecuteMsg::UpdateMinterOwnership(_0) => Ok(InnerExecuteMsg::UpdateCreatorOwnership(_0)),
ExecuteMsg::UpdateCreatorOwnership(_0) => Ok(InnerExecuteMsg::UpdateCreatorOwnership(_0)),
ExecuteMsg::UpdateCollectionInfo { collection_info } => {
Ok(InnerExecuteMsg::UpdateCollectionInfo { collection_info })
}
ExecuteMsg::TransferNft {
recipient,
token_id,
} => Ok(InnerExecuteMsg::TransferNft {
recipient,
token_id,
}),
ExecuteMsg::SendNft {
contract,
token_id,
msg,
} => Ok(InnerExecuteMsg::SendNft {
contract,
token_id,
msg,
}),
ExecuteMsg::Approve {
spender,
token_id,
expires,
} => Ok(InnerExecuteMsg::Approve {
spender,
token_id,
expires,
}),
ExecuteMsg::Revoke { spender, token_id } => {
Ok(InnerExecuteMsg::Revoke { spender, token_id })
}
ExecuteMsg::ApproveAll { operator, expires } => {
Ok(InnerExecuteMsg::ApproveAll { operator, expires })
}
ExecuteMsg::RevokeAll { operator } => Ok(InnerExecuteMsg::RevokeAll { operator }),
ExecuteMsg::Mint {
token_id,
owner,
token_uri,
extension,
} => Ok(InnerExecuteMsg::Mint {
token_id,
owner,
token_uri,
extension,
}),
ExecuteMsg::Burn { token_id } => Ok(InnerExecuteMsg::Burn { token_id }),
ExecuteMsg::UpdateExtension { .. } => Err(InvalidMsgType), // cannot convert a proxy msg into an inner msg
ExecuteMsg::UpdateNftInfo {
token_id,
token_uri,
extension,
} => Ok(InnerExecuteMsg::UpdateNftInfo {
token_id,
token_uri,
extension,
}),
ExecuteMsg::SetWithdrawAddress { address } => {
Ok(InnerExecuteMsg::SetWithdrawAddress { address })
}
ExecuteMsg::RemoveWithdrawAddress {} => Ok(InnerExecuteMsg::RemoveWithdrawAddress {}),
ExecuteMsg::WithdrawFunds { amount } => Ok(InnerExecuteMsg::WithdrawFunds { amount }),
}
}
17 changes: 17 additions & 0 deletions contracts/cw721-proxy/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use cosmwasm_std::Addr;
use cw721::extension::Cw721BaseExtensions;
use cw_storage_plus::Item;

pub struct DefaultCw721ProxyContract<'a> {
pub proxy_addr: Item<'a, Addr>,
pub base_contract: Cw721BaseExtensions<'a>,
}

impl Default for DefaultCw721ProxyContract<'static> {
fn default() -> Self {
Self {
proxy_addr: Item::new("proxy_addr"),
base_contract: Cw721BaseExtensions::default(),
}
}
}
27 changes: 27 additions & 0 deletions contracts/proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "proxy"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["cdylib", "rlib"]

[features]
# enable feature if you want to disable entry points
library = []

[dependencies]
cosmwasm-schema = { workspace = true }
cosmwasm-std = { workspace = true }
cw2 = { workspace = true }
cw-storage-plus = { workspace = true }
thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
schemars = { workspace = true }
cosmos-sdk-proto = { workspace = true }

[dev-dependencies]
cw721-proxy = {path = "../cw721-proxy"}
cw721 = {git = "https://github.com/public-awesome/cw-nfts.git", version = "0.19.0"}
30 changes: 30 additions & 0 deletions contracts/proxy/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use crate::error::ContractResult;
use crate::msg::{ExecuteMsg, InstantiateMsg};
use crate::{execute, CONTRACT_NAME, CONTRACT_VERSION};
use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response};

#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> ContractResult<Response> {
cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
execute::init(deps, info, msg.admin, msg.code_ids)
}

#[entry_point]
pub fn execute(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> ContractResult<Response> {
match msg {
ExecuteMsg::ProxyMsgs { msgs } => execute::proxy_msgs(deps, info, msgs),
ExecuteMsg::AddCodeIDs { code_ids } => execute::add_code_ids(deps, info, code_ids),
ExecuteMsg::RemoveCodeIDs { code_ids } => execute::remove_code_ids(deps, info, code_ids),
ExecuteMsg::UpdateAdmin { new_admin } => execute::update_admin(deps, info, new_admin),
}
}
22 changes: 22 additions & 0 deletions contracts/proxy/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#[derive(Debug, thiserror::Error)]
pub enum ContractError {
#[error(transparent)]
Std(#[from] cosmwasm_std::StdError),

#[error(transparent)]
Encode(#[from] cosmos_sdk_proto::prost::EncodeError),

#[error(transparent)]
Decode(#[from] cosmos_sdk_proto::prost::DecodeError),

#[error("unauthorized")]
Unauthorized,

#[error("invalid_code_id")]
InvalidCodeID { contract: String, code_id: u64 },

#[error("invalid_msg_type")]
InvalidMsgType,
}

pub type ContractResult<T> = Result<T, ContractError>;
Loading
Loading