7 releases (4 breaking)

0.5.1 Feb 28, 2023
0.5.0 Feb 11, 2023
0.4.0 Jan 11, 2023
0.3.0 Dec 28, 2022
0.1.1 Dec 14, 2022

#355 in Magic Beans

Download history 862/week @ 2024-01-01 1123/week @ 2024-01-08 1354/week @ 2024-01-15 2595/week @ 2024-01-22 2597/week @ 2024-01-29 2208/week @ 2024-02-05 2640/week @ 2024-02-12 1775/week @ 2024-02-19 1608/week @ 2024-02-26 1728/week @ 2024-03-04 2053/week @ 2024-03-11 2232/week @ 2024-03-18 2219/week @ 2024-03-25 2629/week @ 2024-04-01 1494/week @ 2024-04-08 2038/week @ 2024-04-15

8,610 downloads per month
Used in 86 crates (43 directly)

Apache-2.0

29KB
408 lines

CW Ownable

Utility for controlling ownership of CosmWasm smart contracts.

How to use

Initialize the owner during instantiation using the initialize_owner method provided by this crate:

use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response};
use cw_ownable::OwnershipError;

#[entry_point]
pub fn instantiate(
    deps: DepsMut,
    env: Env,
    _info: MessageInfo,
    msg: InstantiateMsg,
) -> Result<Response<Empty>, OwnershipError> {
    cw_ownable::initialize_owner(deps.storage, deps.api, msg.owner.as_deref())?;
    Ok(Response::new())
}

Use the #[cw_ownable_execute] macro to extend your execute message:

use cosmwasm_schema::cw_serde;
use cw_ownable::cw_ownable_execute;

#[cw_ownable_execute]
#[cw_serde]
enum ExecuteMsg {
    Foo {},
    Bar {},
}

The macro inserts a new variant, UpdateOwnership to the enum:

#[cw_serde]
enum ExecuteMsg {
    UpdateOwnership(cw_ownable::Action),
    Foo {},
    Bar {},
}

Where Action can be one of three:

  • Propose to transfer the contract's ownership to another account
  • Accept the proposed ownership transfer
  • Renounce the ownership, permanently setting the contract's owner to vacant

Handle the messages using the update_ownership function provided by this crate:

use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response};
use cw_ownable::{cw_serde, update_ownership, OwnershipError};

#[entry_point]
pub fn execute(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, OwnershipError> {
    match msg {
        ExecuteMsg::UpdateOwnership(action) => {
            update_ownership(deps, &env.block, &info.sender, action)?;
        }
        _ => unimplemneted!(),
    }
    Ok(Response::new())
}

Use the #[cw_ownable_query] macro to extend your query message:

use cosmwasm_schema::{cw_serde, QueryResponses};
use cw_ownable::cw_ownable_query;

#[cw_ownable_query]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
    #[returns(FooResponse)]
    Foo {},
    #[returns(BarResponse)]
    Bar {},
}

The macro inserts a new variant, Ownership:

#[cw_serde]
#[derive(QueryResponses)]
enum ExecuteMsg {
    #[returns(Ownership<String>)]
    Ownership {},
    #[returns(FooResponse)]
    Foo {},
    #[returns(BarResponse)]
    Bar {},
}

Handle the message using the get_ownership function provided by this crate:

use cosmwasm_std::{entry_point, Deps, Env, Binary};
use cw_ownable::get_ownership;

#[entry_point]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
    match msg {
        QueryMsg::Ownership {} => to_binary(&get_ownership(deps.storage)?),
        _ => unimplemented!(),
    }
}

License

Contents of this crate at or prior to version 0.5.0 are published under GNU Affero General Public License v3 or later; contents after the said version are published under Apache-2.0 license.

Dependencies

~4–5.5MB
~120K SLoC