#cosmwasm #cosmos #ownership #ownable #smart-contracts #controlling #contract

已删除 lr-cw-ownable

用于控制CosmWasm智能合约所有权的实用工具

1 个稳定版本

2.0.0 2024年4月1日

#14#ownable

Apache-2.0

24KB
402

CW Ownable

用于控制CosmWasm智能合约所有权的实用工具。

如何使用

使用此crate提供的initialize_owner方法在实例化期间初始化所有者

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())
}

使用#[cw_ownable_execute]宏扩展您的执行消息

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

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

该宏向枚举中插入一个新的变体,UpdateOwnership

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

其中Action可以是以下三种之一

  • 提议将合约的所有权转让给另一个账户
  • 接受提议的所有权转让
  • 放弃所有权,永久地将合约的所有者设置为空缺

使用此crate提供的update_ownership函数处理消息

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())
}

使用#[cw_ownable_query]宏扩展您的查询消息

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 {},
}

该宏向枚举中插入一个新的变体,Ownership

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

使用此crate提供的get_ownership函数处理消息

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!(),
    }
}

许可证

此crate在版本0.5.0或之前的内容发布在GNU Affero General Public License v3或更高版本下;该版本之后的内容发布在Apache-2.0许可证下。

依赖项

~4–7.5MB
~152K SLoC