1 个不稳定版本

0.1.0 2024年7月8日

#3#所有权

每月下载量:41

MIT 协议

24KB
422

CW Ownable

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

使用方法

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

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

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

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

use cosmwasm_schema::cw_serde;
use nibiru_ownable::cw_ownable_execute;

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

宏将新变体UpdateOwnership插入到枚举中

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

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

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

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

use cosmwasm_std::{entry_point, DepsMut, Env, MessageInfo, Response};
use nibiru_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())
}

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

use cosmwasm_schema::{cw_serde, QueryResponses};
use nibiru_ownable::ownable_query;

#[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 nibiru_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