pub use crate::{
ownable,
proxy,
traits::{
ownable::*,
proxy::*,
},
};
pub use ownable::Internal as _;
pub use proxy::Internal as _;
use openbrush::{
modifiers,
traits::{
Hash,
Storage,
},
};
pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data);
#[derive(Default, Debug)]
#[openbrush::upgradeable_storage(STORAGE_KEY)]
pub struct Data {
pub forward_to: Hash,
}
impl<T: Storage<Data> + Storage<ownable::Data>> Proxy for T {
default fn get_delegate_code(&self) -> Hash {
self.data::<Data>().forward_to
}
#[modifiers(ownable::only_owner)]
default fn change_delegate_code(&mut self, new_code_hash: Hash) -> Result<(), OwnableError> {
let old_code_hash = self.data::<Data>().forward_to.clone();
self.data::<Data>().forward_to = new_code_hash;
self._emit_delegate_code_changed_event(Some(old_code_hash), Some(new_code_hash));
Ok(())
}
}
pub trait Internal {
fn _emit_delegate_code_changed_event(&self, _previous: Option<Hash>, _new: Option<Hash>);
fn _init_with_forward_to(&mut self, forward_to: Hash);
fn _fallback(&self) -> !;
}
impl<T: Storage<Data>> Internal for T {
default fn _emit_delegate_code_changed_event(&self, _previous: Option<Hash>, _new: Option<Hash>) {}
default fn _init_with_forward_to(&mut self, forward_to: Hash) {
self.data().forward_to = forward_to;
self._emit_delegate_code_changed_event(None, Some(forward_to));
}
default fn _fallback(&self) -> ! {
ink::env::call::build_call::<ink::env::DefaultEnvironment>()
.delegate(self.data().forward_to.clone())
.call_flags(
ink::env::CallFlags::default()
.set_forward_input(true)
.set_tail_call(true),
)
.try_invoke()
.unwrap_or_else(|err| {
panic!(
"delegate call to {:?} failed due to {:?}",
self.data().forward_to.clone(),
err
)
})
.unwrap_or_else(|err| {
panic!(
"delegate call to {:?} failed due to {:?}",
self.data().forward_to.clone(),
err
)
});
unreachable!("the _fallback call will never return since `tail_call` was set");
}
}