use crate::checked_transaction::Checked;
use crate::state::StateTransitionRef;
use crate::storage::MemoryStorage;
use crate::transactor::Transactor;
use crate::{backtrace::Backtrace, gas::GasCosts};
use fuel_tx::{ConsensusParameters, Create, Receipt, Script};
#[derive(Default, Debug)]
pub struct MemoryClient {
transactor: Transactor<MemoryStorage, Script>,
}
impl AsRef<MemoryStorage> for MemoryClient {
fn as_ref(&self) -> &MemoryStorage {
self.transactor.as_ref()
}
}
impl AsMut<MemoryStorage> for MemoryClient {
fn as_mut(&mut self) -> &mut MemoryStorage {
self.transactor.as_mut()
}
}
impl MemoryClient {
pub fn new(storage: MemoryStorage, params: ConsensusParameters, gas_costs: GasCosts) -> Self {
Self {
transactor: Transactor::new(storage, params, gas_costs),
}
}
pub fn from_txtor(transactor: Transactor<MemoryStorage, Script>) -> Self {
Self { transactor }
}
pub fn backtrace(&self) -> Option<Backtrace> {
self.transactor.backtrace()
}
pub fn receipts(&self) -> Option<&[Receipt]> {
self.transactor.receipts()
}
pub fn state_transition(&self) -> Option<StateTransitionRef<'_, Script>> {
self.transactor.state_transition()
}
pub fn deploy(&mut self, tx: Checked<Create>) -> Option<Create> {
self.transactor.deploy(tx).ok()
}
pub fn transact(&mut self, tx: Checked<Script>) -> &[Receipt] {
self.transactor.transact(tx);
if let Ok(state) = self.transactor.result() {
if state.should_revert() {
self.transactor.as_mut().revert();
} else {
self.transactor.as_mut().commit();
}
} else {
self.transactor.as_mut().revert();
}
self.transactor.receipts().unwrap_or_default()
}
pub fn persist(&mut self) {
self.as_mut().persist();
}
pub const fn params(&self) -> &ConsensusParameters {
self.transactor.params()
}
pub const fn tx_offset(&self) -> usize {
self.transactor.tx_offset()
}
pub fn gas_costs(&self) -> &GasCosts {
self.transactor.gas_costs()
}
}
impl From<MemoryStorage> for MemoryClient {
fn from(s: MemoryStorage) -> Self {
Self::new(s, Default::default(), Default::default())
}
}
impl From<MemoryClient> for Transactor<MemoryStorage, Script> {
fn from(client: MemoryClient) -> Self {
client.transactor
}
}