Expand description
§nibiru-ownable
Utility for single-party ownership of CosmWasm smart contracts.
nibiru-ownable provides a comprehensive ownership management system for CosmWasm smart contracts, inspired by OpenZeppelin’s Ownable pattern for Ethereum. It implements a two-phase ownership transfer mechanism with optional expiration deadlines, designed for the CosmWasm execution model.
§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 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())
}Use the #[ownable_execute] macro to extend your execute message:
use cosmwasm_schema::cw_serde;
use nibiru_ownable::ownable_execute;
#[ownable_execute]
#[cw_serde]
enum ExecuteMsg {
Foo {},
Bar {},
}The macro inserts a new variant, UpdateOwnership to the enum:
#[cw_serde]
enum ExecuteMsg {
UpdateOwnership(nibiru_ownable::Action),
Foo {},
Bar {},
}Where nibiru_ownable::Action is an enum with three variants:
Action::TransferOwnership { new_owner: String, expiry: Option<Expiration> }- Propose to transfer ownership with optional deadlineAction::AcceptOwnership- Accept the proposed ownership transferAction::RenounceOwnership- Renounce ownership permanently, setting the contract’s owner to None
Handle the messages using the update_ownership function provided by this crate:
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())
}Use the #[ownable_query] macro to extend your query message:
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 {},
}The macro inserts a new variant, Ownership:
#[cw_serde]
#[derive(QueryResponses)]
enum QueryMsg {
#[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 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!(),
}
}§Core Types
nibiru_ownable::Ownership<T>- Struct containing current owner, pending owner, and expirynibiru_ownable::Action- Enum for ownership management actionsnibiru_ownable::OwnershipError- Error types for ownership operations
§Documentation
For detailed API documentation, visit docs.rs/nibiru-ownable.
§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.
Modules§
Structs§
- Ownership
- The contract’s ownership info
Enums§
- Action
- Actions that can be taken to alter the contract’s ownership
- Expiration
- Expiration represents a point in time when some event happens. It can compare with a BlockInfo and will return is_expired() == true once the condition is hit (and for every block in the future)
- Ownership
Error - Errors associated with the contract’s ownership
Functions§
- assert_
owner - Assert that an account is the contract’s current owner.
- get_
ownership - Get the current ownership value.
- initialize_
owner - Set the given address as the contract owner.
- is_
owner - Return Ok(true) if the contract has an owner and it’s the given address. Return Ok(false) if the contract doesn’t have an owner, of if it does but it’s not the given address. Return Err if fails to load ownership info from storage.
- update_
ownership - Update the contract’s ownership info based on the given action. Return the updated ownership.
Attribute Macros§
- ownable_
execute - Append ownership-related execute message variant(s) to an enum.
- ownable_
query - Append ownership-related query message variant(s) to an enum.