use loam_sdk::{
soroban_sdk::{self, contracttype, env, symbol_short, Address, BytesN, Lazy, Symbol},
subcontract,
};
#[contracttype(export = false)]
#[derive(Default)]
pub struct Admin(Kind);
fn admin_key() -> Symbol {
symbol_short!("ADMIN")
}
impl Lazy for Admin {
fn get_lazy() -> Option<Self> {
env().storage().instance().get(&admin_key())
}
fn set_lazy(self) {
env().storage().instance().set(&admin_key(), &self);
}
}
#[contracttype(export = false)]
#[derive(Default)]
pub enum Kind {
Address(Address),
#[default]
None,
}
impl IsCore for Admin {
fn admin_get(&self) -> Option<Address> {
match &self.0 {
Kind::Address(address) => Some(address.clone()),
Kind::None => None,
}
}
fn admin_set(&mut self, new_admin: Address) {
if let Admin(Kind::Address(admin)) = &self {
admin.require_auth();
}
self.0 = Kind::Address(new_admin);
}
fn redeploy(&self, wasm_hash: BytesN<32>) {
self.admin_get().unwrap().require_auth();
env().deployer().update_current_contract_wasm(wasm_hash);
}
}
#[subcontract]
pub trait IsCore {
fn admin_get(&self) -> Option<loam_sdk::soroban_sdk::Address>;
fn admin_set(&mut self, new_admin: loam_sdk::soroban_sdk::Address);
fn redeploy(&self, wasm_hash: loam_sdk::soroban_sdk::BytesN<32>);
}