use cosmwasm_std::{attr, DepsMut, Env, MessageInfo, Response, StdError, StdResult};
use cw_storage_plus::Map;
use serde::de::DeserializeOwned;
use serde::Serialize;

use crate::error::ContractError;
use crate::state::{Config, CONFIG, ENTRY_SEQ};

pub fn instantiate(
    deps: DepsMut,
    info: MessageInfo,
    owner: Option<String>,
) -> Result<(Config, Response), ContractError> {
    let owner_addr = match owner {
        Some(addr_string) => deps.api.addr_validate(addr_string.as_str())?,
        None => info.sender,
    };

    let config = Config {
        owner: Some(owner_addr.clone()),
    };
    CONFIG.save(deps.storage, &config)?;
    ENTRY_SEQ.save(deps.storage, &0u64)?;

    Ok((
        config,
        Response::new()
            .add_attribute("method", "instantiate")
            .add_attribute("owner", owner_addr),
    ))
}

pub fn execute_update_owner(
    deps: DepsMut,
    _env: Env,
    info: MessageInfo,
    new_owner: Option<String>,
) -> Result<Response, ContractError> {
    let cfg = CONFIG.load(deps.storage)?;
    let owner = cfg.owner.ok_or(ContractError::Unauthorized {})?;
    if info.sender != owner {
        return Err(ContractError::Unauthorized {});
    }

    let mut tmp_owner = None;
    if let Some(addr) = new_owner {
        tmp_owner = Some(deps.api.addr_validate(&addr)?)
    }

    CONFIG.update(deps.storage, |mut exists| -> StdResult<_> {
        exists.owner = tmp_owner;
        Ok(exists)
    })?;

    Ok(Response::new().add_attributes(vec![attr("action", "update_owner")]))
}

pub fn execute_delete_entry<E: Serialize + DeserializeOwned>(
    deps: DepsMut,
    info: MessageInfo,
    id: u64,
    list: &Map<u64, E>,
) -> Result<Response, ContractError> {
    let owner = CONFIG
        .load(deps.storage)?
        .owner
        .ok_or(ContractError::Unauthorized {})?;
    if info.sender != owner {
        return Err(ContractError::Unauthorized {});
    }

    list.remove(deps.storage, id);

    Ok(Response::new()
        .add_attribute("method", "execute_delete_entry")
        .add_attribute("deleted_entry_id", id.to_string()))
}

/// Check that the sender is the owner. Returns Err(Unauthorized) if not.
pub fn check_owner(deps: &DepsMut, info: &MessageInfo) -> Result<(), ContractError> {
    let owner = CONFIG
        .load(deps.storage)?
        .owner
        .ok_or(ContractError::Unauthorized {})?;
    if info.sender != owner {
        return Err(ContractError::Unauthorized {});
    }
    Ok(())
}

/// Allocate the next entry ID from the sequence.
pub fn next_entry_id(deps: &mut DepsMut) -> Result<u64, ContractError> {
    let id = ENTRY_SEQ.update::<_, cosmwasm_std::StdError>(deps.storage, |id| {
        id.checked_add(1)
            .ok_or_else(|| StdError::generic_err("entry sequence overflow"))
    })?;
    Ok(id)
}

Dimensions

trident/src/neural/inference/execute.rs
cw-cyber/contracts/hub-channels/src/execute.rs
cw-cyber/contracts/hub-skills/src/execute.rs
cw-cyber/contracts/graph-filter/src/execute.rs
cw-cyber/contracts/cw-cyber-gift/src/execute.rs
cw-cyber/contracts/cw-cyber-subgraph/src/execute.rs
cw-cyber/contracts/hub-tokens/src/execute.rs
cw-cyber/contracts/hub-libs/src/execute.rs
cw-cyber/contracts/hub-protocols/src/execute.rs
cw-cyber/contracts/hub-networks/src/execute.rs
cw-cyber/contracts/cw-cyber-passport/src/execute.rs

Local Graph