Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gists-immunefi/5904c5f1209e78506ba43ebf1d891549 to your computer and use it in GitHub Desktop.
Save gists-immunefi/5904c5f1209e78506ba43ebf1d891549 to your computer and use it in GitHub Desktop.
// main.sw
contract;
mod ownable;
use ownable::*;
storage {
owner: b256 = b256::zero(),
}
abi MyAbi : Ownable {
#[storage(read, write)]
fn init();
#[storage(read)]
fn owner() -> b256;
}
impl StorageHelpers for Contract {
#[storage(read)]
fn get_owner() -> b256 {
storage.owner.read()
}
#[storage(write)]
fn set_owner(owner: b256) {
storage.owner.write(owner)
}
}
impl Ownable for Contract { }
impl MyAbi for Contract {
#[storage(read, write)]
fn init() {
Self::set_owner(ContractId::this().into());
}
#[storage(read)]
fn owner() -> b256 {
return Self::get_owner();
}
}
abi Test {
fn renounce_ownership();
}
#[test]
fn test() {
let callerA = abi(MyAbi, CONTRACT_ID);
callerA.init();
assert(callerA.owner() == CONTRACT_ID);
let callerB = abi(Test, CONTRACT_ID);
callerB.renounce_ownership(); // ! Externally available
assert(callerA.owner() != CONTRACT_ID);
assert(callerA.owner() == b256::zero());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment