mod address;
use crate::client::OfflineClientAtBlockT;
use crate::config::Config;
use crate::error::CustomValueError;
use crate::utils::Maybe;
use derive_where::derive_where;
use frame_decode::custom_values::CustomValueTypeInfo;
use scale_decode::IntoVisitor;
pub use address::{Address, DynamicAddress, StaticAddress, dynamic};
#[derive_where(Clone; Client)]
pub struct CustomValuesClient<'atblock, T, Client> {
client: &'atblock Client,
marker: std::marker::PhantomData<T>,
}
impl<'atblock, T, Client> CustomValuesClient<'atblock, T, Client> {
pub(crate) fn new(client: &'atblock Client) -> Self {
Self {
client,
marker: std::marker::PhantomData,
}
}
}
impl<'atblock, T: Config, Client: OfflineClientAtBlockT<T>>
CustomValuesClient<'atblock, T, Client>
{
pub fn validate<Addr: Address>(&self, address: Addr) -> Result<(), CustomValueError> {
let metadata = self.client.metadata_ref();
if let Some(actual_hash) = address.validation_hash() {
let custom = metadata.custom();
let custom_value = custom
.get(address.name())
.ok_or_else(|| CustomValueError::NotFound(address.name().into()))?;
let expected_hash = custom_value.hash();
if actual_hash != expected_hash {
return Err(CustomValueError::IncompatibleCodegen);
}
}
Ok(())
}
pub fn entry<Addr: Address<IsDecodable = Maybe>>(
&self,
address: Addr,
) -> Result<Addr::Target, CustomValueError> {
self.validate(&address)?;
let metadata = self.client.metadata_ref();
let value = frame_decode::custom_values::decode_custom_value(
address.name(),
metadata,
metadata.types(),
Addr::Target::into_visitor(),
)
.map_err(CustomValueError::CouldNotDecodeCustomValue)?;
Ok(value)
}
pub fn entry_bytes<Addr: Address>(&self, address: Addr) -> Result<Vec<u8>, CustomValueError> {
self.validate(&address)?;
let custom_value = self
.client
.metadata_ref()
.custom_value_info(address.name())
.map_err(|e| CustomValueError::NotFound(e.not_found))?;
Ok(custom_value.bytes.to_vec())
}
}