use super::{error::ContextError, TransactionContext};
use sabre_sdk::TransactionContext as SabreTransactionContext;
pub struct SabreContext<'a> {
pub context: &'a mut dyn SabreTransactionContext,
}
impl<'a> TransactionContext for SabreContext<'a> {
fn get_state_entry(&self, address: &str) -> Result<Option<Vec<u8>>, ContextError> {
Ok(self
.context
.get_state_entries(&[address.to_string()])
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))?
.into_iter()
.map(|(_, val)| val)
.next())
}
fn get_state_entries(
&self,
addresses: &[String],
) -> Result<Vec<(String, Vec<u8>)>, ContextError> {
self.context
.get_state_entries(addresses)
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))
}
fn set_state_entry(&self, address: String, data: Vec<u8>) -> Result<(), ContextError> {
self.context
.set_state_entries(vec![(address, data)])
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))
}
fn set_state_entries(&self, entries: Vec<(String, Vec<u8>)>) -> Result<(), ContextError> {
self.context
.set_state_entries(entries)
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))
}
fn delete_state_entry(&self, address: &str) -> Result<Option<String>, ContextError> {
Ok(self
.delete_state_entries(&[address.to_string()])
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))?
.into_iter()
.next())
}
fn delete_state_entries(&self, addresses: &[String]) -> Result<Vec<String>, ContextError> {
self.context
.delete_state_entries(addresses)
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))
}
fn add_receipt_data(&self, _data: Vec<u8>) -> Result<(), ContextError> {
unimplemented!()
}
fn add_event(
&self,
event_type: String,
attributes: Vec<(String, String)>,
data: Vec<u8>,
) -> Result<(), ContextError> {
self.context
.add_event(event_type, attributes, &data)
.map_err(|err| ContextError::ResponseAttributeError(err.to_string()))
}
}