Skip to content

Commit

Permalink
feat(storage): add execute PinObject
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Mar 8, 2023
1 parent 55b02d7 commit 07f5e37
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn execute(
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::StoreObject { data, pin } => execute::store_object(deps, info, data, pin),
ExecuteMsg::PinObject { id } => execute::pin_object(deps, info, id),
_ => Err(NotImplemented {}),
}
}
Expand Down Expand Up @@ -116,6 +117,37 @@ pub mod execute {

Ok(res)
}

pub fn pin_object(
deps: DepsMut,
info: MessageInfo,
objectId: ObjectId,
) -> Result<Response, ContractError> {
let bucket = BUCKET.load(deps.storage)?;
let pins_count = pins().prefix(objectId.clone()).keys_raw(deps.storage, None, None, Ascending).count();

match bucket.limits {
Limits {
max_object_pins: Some(max),
..
} if max < Uint128::new(u128::try_from(pins_count).unwrap()) => {
Err(BucketError::MaxObjectPinsLimitExceeded.into())
}
_ => {
pins().save(
deps.storage,
(objectId.clone(), info.sender.clone()),
&Pin {
id: objectId.clone(),
address: info.sender,
},
)?;
Ok(Response::new()
.add_attribute("action", "pin_object")
.add_attribute("id", objectId.clone()))
}
}
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down

0 comments on commit 07f5e37

Please sign in to comment.