-
Notifications
You must be signed in to change notification settings - Fork 14
Authz Code ID Proxy Contract Example #35
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
Draft
ash-burnt
wants to merge
8
commits into
main
Choose a base branch
from
feat/proxy-authz
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c92275c
initial proxy contract
ash-burnt 6a97cf7
example proxyable contract
ash-burnt 6876b02
cleanup
ash-burnt 3cdc817
linting
ash-burnt 6005a4d
fmt
ash-burnt 45017dd
clear unused var
ash-burnt 02133aa
linting
ash-burnt d0b4c1f
example
ash-burnt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use crate::msg::{get_inner, ExecuteMsg, InnerExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}; | ||
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)?) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
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 }), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.