use codec::{Encode};
use frame_support::sp_io::hashing::blake2_256;
use sp_std::prelude::*;
fn selector_from_str(callback: &str) -> Vec<u8> {
let hash = blake2_256(callback.as_bytes());
[hash[0], hash[1], hash[2], hash[3]].to_vec()
}
pub struct ContractDeployment<AccountId> {
pub creator: AccountId,
pub code: Vec<u8>,
pub init_args: Vec<u8>,
pub salt: Vec<u8>
}
impl<AccountId> ContractDeployment<AccountId> {
pub fn new(constructor: &str, creator: AccountId, code: Vec<u8>, salt: Vec<u8>) -> Self {
ContractDeployment {
creator,
code,
salt,
init_args: selector_from_str(constructor)
}
}
pub fn add_init_arg<T>(mut self, arg: T) -> Self
where
T: Encode
{
self.init_args.append(&mut arg.encode());
self
}
}
pub struct HookPoint<AccountId> {
pub owner: AccountId,
pub signer: AccountId,
pub callback: Vec<u8>,
pub data: Vec<u8>
}
impl<AccountId> HookPoint<AccountId> {
pub fn new(callback: &str, owner: AccountId, origin: AccountId) -> Self {
HookPoint {
owner,
signer: origin,
callback: callback.into(),
data: selector_from_str(callback)
}
}
pub fn add_arg<T>(mut self, arg: T) -> Self
where
T: Encode
{
self.data.append(&mut arg.encode());
self
}
}