Skip to content

Commit

Permalink
XcRegions: fix remove (#18)
Browse files Browse the repository at this point in the history
* XcRegions: fix remove

* docs formatting fix

* ..
  • Loading branch information
Szegoo authored Jan 17, 2024
1 parent ce56d1f commit 403d89b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
3 changes: 2 additions & 1 deletion contracts/xc-regions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub mod xc_regions {
/// This process involves burning the wrapped region and eliminating its associated
/// metadata.
///
///Only the owner of the wrapped region can call this function.
/// Only the owner of the wrapped region can call this function.
///
/// ## Arguments:
/// - `raw_region_id` - The `u128` encoded region identifier.
Expand All @@ -212,6 +212,7 @@ pub mod xc_regions {
let owner =
psp34::PSP34Impl::owner_of(self, id.clone()).ok_or(XcRegionsError::CannotRemove)?;

ensure!(owner == self.env().caller(), XcRegionsError::CannotRemove);
self.regions.remove(region_id);

psp34::InternalImpl::_burn_from(self, owner, id).map_err(XcRegionsError::Psp34)?;
Expand Down
7 changes: 6 additions & 1 deletion contracts/xc-regions/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn init_works() {

#[ink::test]
fn remove_works() {
let DefaultAccounts::<DefaultEnvironment> { charlie, .. } = get_default_accounts();
let DefaultAccounts::<DefaultEnvironment> { bob, charlie, .. } = get_default_accounts();
let mut xc_regions = XcRegions::new();
set_caller::<DefaultEnvironment>(charlie);

Expand All @@ -143,6 +143,11 @@ fn remove_works() {
assert_eq!(xc_regions.regions.get(0), Some(Region::default()));
assert_eq!(xc_regions.metadata_versions.get(0), Some(0));

// Only charlie can remove the region:
set_caller::<DefaultEnvironment>(bob);
assert_eq!(xc_regions.remove(0), Err(XcRegionsError::CannotRemove));

set_caller::<DefaultEnvironment>(charlie);
// Removing a region works:
assert_ok!(xc_regions.remove(0));

Expand Down

0 comments on commit 403d89b

Please sign in to comment.