Skip to content

Commit

Permalink
feat(storage): implement the forget object execute message
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 21, 2023
1 parent 9af7b34 commit 52f1666
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
35 changes: 34 additions & 1 deletion contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ pub fn execute(
ExecuteMsg::StoreObject { data, pin } => execute::store_object(deps, info, data, pin),
ExecuteMsg::PinObject { id } => execute::pin_object(deps, info, id),
ExecuteMsg::UnpinObject { id } => execute::unpin_object(deps, info, id),
ExecuteMsg::ForgetObject { id } => execute::forget_object(deps, info, id),
_ => Err(NotImplemented {}),
}
}

pub mod execute {
use super::*;
use crate::state::Limits;
use cosmwasm_std::{StdError, Uint128};
use crate::ContractError::Pinned;
use cosmwasm_std::Order::Ascending;
use cosmwasm_std::StdError::NotFound;
use cosmwasm_std::{Order, StdError, Uint128};
use cw_storage_plus::Bound;
use std::any::type_name;

pub fn store_object(
Expand Down Expand Up @@ -195,6 +200,34 @@ pub mod execute {

Ok(res)
}

pub fn forget_object(
deps: DepsMut,
_info: MessageInfo,
object_id: ObjectId,
) -> Result<Response, ContractError> {
if pins()
.idx
.object
.prefix(object_id.clone())
.keys_raw(deps.storage, None, None, Order::Ascending)
.next()
.is_some()
{
return Err(Pinned {});
}
let object = query::object(deps.as_ref(), object_id.clone())?;
BUCKET.update(deps.storage, |mut b| -> Result<_, ContractError> {
b.stat.object_count -= Uint128::one();
b.stat.size -= object.size;
Ok(b)
})?;

objects().remove(deps.storage, object_id.clone())?;
Ok(Response::new()
.add_attribute("action", "forget_object")
.add_attribute("id", object_id))
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down
3 changes: 3 additions & 0 deletions contracts/cw-storage/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub enum ContractError {

#[error("{0}")]
Bucket(#[from] BucketError),

#[error("Object is pinned")]
Pinned {},
}

#[derive(Error, Debug, Eq, PartialEq)]
Expand Down

0 comments on commit 52f1666

Please sign in to comment.