use crate::{
call::{
utils::{
EmptyArgumentList,
ReturnType,
Set,
Unset,
Unwrap,
},
ExecutionInput,
},
Environment,
Error,
};
use core::marker::PhantomData;
pub mod state {
pub enum Salt {}
}
pub trait FromAccountId<T>
where
T: Environment,
{
fn from_account_id(account_id: <T as Environment>::AccountId) -> Self;
}
#[derive(Debug)]
pub struct CreateParams<E, Args, Salt, R>
where
E: Environment,
{
code_hash: E::Hash,
gas_limit: u64,
endowment: E::Balance,
exec_input: ExecutionInput<Args>,
salt_bytes: Salt,
_return_type: ReturnType<R>,
}
impl<E, Args, Salt, R> CreateParams<E, Args, Salt, R>
where
E: Environment,
{
#[inline]
pub(crate) fn code_hash(&self) -> &E::Hash {
&self.code_hash
}
#[inline]
pub(crate) fn gas_limit(&self) -> u64 {
self.gas_limit
}
#[inline]
pub(crate) fn endowment(&self) -> &E::Balance {
&self.endowment
}
#[inline]
pub(crate) fn exec_input(&self) -> &ExecutionInput<Args> {
&self.exec_input
}
}
impl<E, Args, Salt, R> CreateParams<E, Args, Salt, R>
where
E: Environment,
Salt: AsRef<[u8]>,
{
#[inline]
pub(crate) fn salt_bytes(&self) -> &Salt {
&self.salt_bytes
}
}
impl<E, Args, Salt, R> CreateParams<E, Args, Salt, R>
where
E: Environment,
Args: scale::Encode,
Salt: AsRef<[u8]>,
R: FromAccountId<E>,
{
#[inline]
pub fn instantiate(&self) -> Result<R, crate::Error> {
crate::instantiate_contract(self).map(FromAccountId::from_account_id)
}
}
pub struct CreateBuilder<E, CodeHash, GasLimit, Endowment, Args, Salt, R>
where
E: Environment,
{
code_hash: CodeHash,
gas_limit: GasLimit,
endowment: Endowment,
exec_input: Args,
salt: Salt,
return_type: ReturnType<R>,
_phantom: PhantomData<fn() -> E>,
}
#[allow(clippy::type_complexity)]
pub fn build_create<E, R>() -> CreateBuilder<
E,
Unset<E::Hash>,
Unset<u64>,
Unset<E::Balance>,
Unset<ExecutionInput<EmptyArgumentList>>,
Unset<state::Salt>,
R,
>
where
E: Environment,
R: FromAccountId<E>,
{
CreateBuilder {
code_hash: Default::default(),
gas_limit: Default::default(),
endowment: Default::default(),
exec_input: Default::default(),
salt: Default::default(),
return_type: Default::default(),
_phantom: Default::default(),
}
}
impl<E, GasLimit, Endowment, Args, Salt, R>
CreateBuilder<E, Unset<E::Hash>, GasLimit, Endowment, Args, Salt, R>
where
E: Environment,
{
#[inline]
pub fn code_hash(
self,
code_hash: E::Hash,
) -> CreateBuilder<E, Set<E::Hash>, GasLimit, Endowment, Args, Salt, R> {
CreateBuilder {
code_hash: Set(code_hash),
gas_limit: self.gas_limit,
endowment: self.endowment,
exec_input: self.exec_input,
salt: self.salt,
return_type: self.return_type,
_phantom: Default::default(),
}
}
}
impl<E, CodeHash, Endowment, Args, Salt, R>
CreateBuilder<E, CodeHash, Unset<u64>, Endowment, Args, Salt, R>
where
E: Environment,
{
#[inline]
pub fn gas_limit(
self,
gas_limit: u64,
) -> CreateBuilder<E, CodeHash, Set<u64>, Endowment, Args, Salt, R> {
CreateBuilder {
code_hash: self.code_hash,
gas_limit: Set(gas_limit),
endowment: self.endowment,
exec_input: self.exec_input,
salt: self.salt,
return_type: self.return_type,
_phantom: Default::default(),
}
}
}
impl<E, CodeHash, GasLimit, Args, Salt, R>
CreateBuilder<E, CodeHash, GasLimit, Unset<E::Balance>, Args, Salt, R>
where
E: Environment,
{
#[inline]
pub fn endowment(
self,
endowment: E::Balance,
) -> CreateBuilder<E, CodeHash, GasLimit, Set<E::Balance>, Args, Salt, R> {
CreateBuilder {
code_hash: self.code_hash,
gas_limit: self.gas_limit,
endowment: Set(endowment),
exec_input: self.exec_input,
salt: self.salt,
return_type: self.return_type,
_phantom: Default::default(),
}
}
}
impl<E, CodeHash, GasLimit, Endowment, Salt, R>
CreateBuilder<
E,
CodeHash,
GasLimit,
Endowment,
Unset<ExecutionInput<EmptyArgumentList>>,
Salt,
R,
>
where
E: Environment,
{
#[inline]
pub fn exec_input<Args>(
self,
exec_input: ExecutionInput<Args>,
) -> CreateBuilder<E, CodeHash, GasLimit, Endowment, Set<ExecutionInput<Args>>, Salt, R>
{
CreateBuilder {
code_hash: self.code_hash,
gas_limit: self.gas_limit,
endowment: self.endowment,
exec_input: Set(exec_input),
salt: self.salt,
return_type: self.return_type,
_phantom: Default::default(),
}
}
}
impl<E, CodeHash, GasLimit, Endowment, Args, R>
CreateBuilder<E, CodeHash, GasLimit, Endowment, Args, Unset<state::Salt>, R>
where
E: Environment,
{
#[inline]
pub fn salt_bytes<Salt>(
self,
salt: Salt,
) -> CreateBuilder<E, CodeHash, GasLimit, Endowment, Args, Set<Salt>, R>
where
Salt: AsRef<[u8]>,
{
CreateBuilder {
code_hash: self.code_hash,
gas_limit: self.gas_limit,
endowment: self.endowment,
exec_input: self.exec_input,
salt: Set(salt),
return_type: self.return_type,
_phantom: Default::default(),
}
}
}
impl<E, GasLimit, Args, Salt, R>
CreateBuilder<
E,
Set<E::Hash>,
GasLimit,
Set<E::Balance>,
Set<ExecutionInput<Args>>,
Set<Salt>,
R,
>
where
E: Environment,
GasLimit: Unwrap<Output = u64>,
{
#[inline]
pub fn params(self) -> CreateParams<E, Args, Salt, R> {
CreateParams {
code_hash: self.code_hash.value(),
gas_limit: self.gas_limit.unwrap_or_else(|| 0),
endowment: self.endowment.value(),
exec_input: self.exec_input.value(),
salt_bytes: self.salt.value(),
_return_type: self.return_type,
}
}
}
impl<E, GasLimit, Args, Salt, R>
CreateBuilder<
E,
Set<E::Hash>,
GasLimit,
Set<E::Balance>,
Set<ExecutionInput<Args>>,
Set<Salt>,
R,
>
where
E: Environment,
GasLimit: Unwrap<Output = u64>,
Args: scale::Encode,
Salt: AsRef<[u8]>,
R: FromAccountId<E>,
{
#[inline]
pub fn instantiate(self) -> Result<R, Error> {
self.params().instantiate()
}
}