Skip to content

Commit

Permalink
move to internal check_owner() for re-use (#15)
Browse files Browse the repository at this point in the history
* move to internal check_owner() for re-use

* revert formatting

---------

Co-authored-by: Tai 'Mr. T' Truong <[email protected]>
  • Loading branch information
taitruong and Tai 'Mr. T' Truong authored Jul 26, 2023
1 parent 38787d9 commit eed2b87
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions packages/ownable/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub fn is_owner(store: &dyn Storage, addr: &Addr) -> StdResult<bool> {

if let Some(owner) = ownership.owner {
if *addr == owner {
return Ok(true)
return Ok(true);
}
}

Expand All @@ -117,7 +117,11 @@ pub fn is_owner(store: &dyn Storage, addr: &Addr) -> StdResult<bool> {
/// Assert that an account is the contract's current owner.
pub fn assert_owner(store: &dyn Storage, sender: &Addr) -> Result<(), OwnershipError> {
let ownership = OWNERSHIP.load(store)?;
check_owner(&ownership, sender)
}

/// Assert that an account is the contract's current owner.
fn check_owner(ownership: &Ownership<Addr>, sender: &Addr) -> Result<(), OwnershipError> {
// the contract must have an owner
let Some(current_owner) = &ownership.owner else {
return Err(OwnershipError::NoOwner);
Expand Down Expand Up @@ -212,14 +216,7 @@ fn transfer_ownership(
) -> Result<Ownership<Addr>, OwnershipError> {
OWNERSHIP.update(deps.storage, |ownership| {
// the contract must have an owner
let Some(current_owner) = ownership.owner else {
return Err(OwnershipError::NoOwner);
};

// the sender must be the current owner
if *sender != current_owner {
return Err(OwnershipError::NotOwner);
}
check_owner(&ownership, sender)?;

// NOTE: We don't validate the expiry, i.e. asserting it is later than
// the current block time.
Expand All @@ -233,9 +230,9 @@ fn transfer_ownership(
// To fix the erorr, the owner can simply invoke `transfer_ownership`
// again with the correct expiry and overwrite the invalid one.
Ok(Ownership {
owner: Some(current_owner),
pending_owner: Some(deps.api.addr_validate(new_owner)?),
pending_expiry: expiry,
..ownership
})
})
}
Expand Down Expand Up @@ -278,15 +275,7 @@ fn renounce_ownership(
sender: &Addr,
) -> Result<Ownership<Addr>, OwnershipError> {
OWNERSHIP.update(store, |ownership| {
// the contract must have an owner
let Some(current_owner) = &ownership.owner else {
return Err(OwnershipError::NoOwner);
};

// the sender must be the current owner
if sender != current_owner {
return Err(OwnershipError::NotOwner);
}
check_owner(&ownership, sender)?;

Ok(Ownership {
owner: None,
Expand Down

0 comments on commit eed2b87

Please sign in to comment.