use super::errors::Error;
use super::events::DappRegistryChanged;
use super::storage::DappRegistryStorage;
use super::DappRegistryContractRef;
use odra::prelude::*;
use odra::ContractRef;
#[odra::external_contract]
pub trait DappContract {
fn get_dapp_registry(&self) -> Address;
}
#[odra::module(events = [DappRegistryChanged], errors = Error)]
pub struct DappContractBase {
registry: SubModule<DappRegistryStorage>
}
#[odra::module]
impl DappContractBase {
pub fn get_dapp_registry(&self) -> Address {
self.registry
.get()
.unwrap_or_revert_with(&self.env(), Error::DappRegistryNotSet)
}
}
impl DappContractBase {
pub fn set_dapp_registry(&mut self, registry: &Address) {
if !registry.is_contract() {
self.env().revert(Error::NotAContract);
}
let previous_registry = self.registry.get();
self.registry.set(*registry);
self.env().emit_event(DappRegistryChanged {
previous_registry,
new_registry: *registry
});
}
pub fn register_address(&self) -> Address {
self.get_dapp_registry()
}
pub fn register(&self, dapp_contract: Address) {
DappRegistryContractRef::new(self.env(), self.get_dapp_registry())
.add_dapp_contract(&dapp_contract, false);
}
}
#[cfg(test)]
mod tests {
use super::*;
use odra::host::{Deployer, NoArgs};
#[test]
fn register_not_set() {
let env = odra_test::odra_env();
let contract = DappContractBase::deploy(&env, NoArgs);
assert_eq!(
contract.try_get_dapp_registry(),
Err(Error::DappRegistryNotSet.into())
);
}
}