use alloc::borrow::Cow;
use codec::Encode;
use corevm_engine::AccumulateOps;
use corevm_host::StorageKey;
use jam_pvm_common::{
accumulate::{bless, get_foreign_storage, remove_storage, set_storage, transfer, zombify},
service_info_field, ApiError,
};
use jam_types::{Balance, Memo, ServiceId, UnsignedGas};
pub struct JamAccumulateOps;
impl AccumulateOps for JamAccumulateOps {
type Error = ApiError;
fn get(&self, service: ServiceId, key: &StorageKey) -> Option<Cow<'_, [u8]>> {
key.using_encoded(|key| get_foreign_storage(service, key)).map(Cow::Owned)
}
fn set(&mut self, key: StorageKey, value: Cow<'_, [u8]>) -> Result<(), Self::Error> {
key.using_encoded(|key| set_storage(key, value.as_ref()))?;
Ok(())
}
fn remove(&mut self, key: &StorageKey) -> bool {
key.using_encoded(|key| remove_storage(key).is_some())
}
fn min_memo_gas(&self, service: ServiceId) -> Option<UnsignedGas> {
service_info_field!(u64::from(service), min_memo_gas)
}
fn transfer(
&self,
destination: ServiceId,
amount: Balance,
gas_limit: UnsignedGas,
memo: &Memo,
) -> Result<(), Self::Error> {
transfer(destination, amount, gas_limit, memo)
}
fn bless<'a>(
&mut self,
manager: ServiceId,
assigner: ServiceId,
designator: ServiceId,
registrar: ServiceId,
always_acc: impl IntoIterator<Item = &'a (ServiceId, UnsignedGas)>,
) {
bless(manager, assigner, designator, registrar, always_acc);
}
fn zombify(&mut self, ejector: ServiceId) {
zombify(ejector);
}
}