use codec::Decode;
use frame_support::BoundedVec;
use super::*;
use frame_support::pallet_prelude::{DispatchError, Get};
use pallet_contracts::{CollectEvents, DebugInfo, Determinism, Pallet as Contracts};
use pallet_contracts_primitives::Code;
use crate::builder::{ContractDeployment, HookPoint};
impl<T: Config> Pallet<T> {
pub fn get_callback(owner: &T::AccountId, callback_name: Vec<u8>) -> Option<T::AccountId> {
let call: BoundedVec<_, _> = callback_name.try_into().unwrap();
Pallet::<T>::specific_callbacks(owner, call)
.or_else(|| Pallet::<T>::callbacks(owner))
}
pub fn execute<R>(hook_point: HookPoint<T::AccountId>) -> Result<R, DispatchError>
where R: Decode
{
let callback = Pallet::<T>::get_callback(&hook_point.owner, hook_point.callback);
let contract = callback.ok_or(DispatchError::Other("no contract"))?;
let data = Contracts::<T>::bare_call(
hook_point.signer,
contract,
0_u32.into(),
T::BlockWeights::get().max_block,
Some(0_u32.into()),
hook_point.data,
DebugInfo::Skip,
CollectEvents::Skip,
Determinism::Enforced,
).result?.data;
<Result<R, DispatchError>>::decode(&mut &data[..])
.map_err(|_| DispatchError::Other("decoding error"))
.unwrap()
}
pub fn install(contract_deployment: ContractDeployment<T::AccountId>) -> Result<T::AccountId, DispatchError> {
let ContractDeployment {
creator,
code,
init_args,
salt,
..
} = contract_deployment;
let contract_instantiate_result = Contracts::<T>::bare_instantiate(
creator,
0_u32.into(),
T::BlockWeights::get().max_block,
Some(100_u32.into()),
Code::Upload(code),
init_args,
salt,
pallet_contracts::DebugInfo::Skip,
pallet_contracts::CollectEvents::Skip,
);
Ok(contract_instantiate_result.result?.account_id)
}
pub fn create(callback: &str, owner: T::AccountId, origin: T::AccountId) -> HookPoint<T::AccountId> {
HookPoint::<T::AccountId>::new(callback, owner, origin)
}
pub fn prepare_deployment(constructor: &str, creator: T::AccountId, code: Vec<u8>, salt: Vec<u8>) -> ContractDeployment<T::AccountId> {
ContractDeployment::<T::AccountId>::new(constructor, creator, code, salt)
}
}