Created
September 25, 2024 10:43
-
-
Save gists-immunefi/5904c5f1209e78506ba43ebf1d891549 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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