use super::Keypair;
use crate::{
backend_calls::{
InstantiateBuilder,
RemoveCodeBuilder,
UploadBuilder,
},
builders::CreateBuilderPartial,
contract_results::{
BareInstantiationResult,
InstantiateDryRunResult,
},
CallBuilder,
CallBuilderFinal,
CallDryRunResult,
UploadResult,
};
use ink_env::{
DefaultEnvironment,
Environment,
};
use jsonrpsee::core::async_trait;
use scale::{
Decode,
Encode,
};
use sp_weights::Weight;
use subxt::dynamic::Value;
#[async_trait]
pub trait E2EBackend<E: Environment = DefaultEnvironment>:
ChainBackend + BuilderClient<E>
{
}
#[async_trait]
pub trait ChainBackend {
type AccountId;
type Balance: Send + From<u32>;
type Error;
type EventLog;
async fn create_and_fund_account(
&mut self,
origin: &Keypair,
amount: Self::Balance,
) -> Keypair;
async fn free_balance(
&mut self,
account: Self::AccountId,
) -> Result<Self::Balance, Self::Error>;
async fn runtime_call<'a>(
&mut self,
origin: &Keypair,
pallet_name: &'a str,
call_name: &'a str,
call_data: Vec<Value>,
) -> Result<Self::EventLog, Self::Error>;
}
#[async_trait]
pub trait ContractsBackend<E: Environment> {
type Error;
type EventLog;
fn instantiate<'a, Contract: Clone, Args: Send + Clone + Encode + Sync, R>(
&'a mut self,
contract_name: &'a str,
caller: &'a Keypair,
constructor: &'a mut CreateBuilderPartial<E, Contract, Args, R>,
) -> InstantiateBuilder<'a, E, Contract, Args, R, Self>
where
Self: Sized + BuilderClient<E>,
{
InstantiateBuilder::new(self, caller, contract_name, constructor)
}
fn upload<'a>(
&'a mut self,
contract_name: &'a str,
caller: &'a Keypair,
) -> UploadBuilder<E, Self>
where
Self: Sized + BuilderClient<E>,
{
UploadBuilder::new(self, contract_name, caller)
}
fn remove_code<'a>(
&'a mut self,
caller: &'a Keypair,
code_hash: E::Hash,
) -> RemoveCodeBuilder<E, Self>
where
Self: Sized + BuilderClient<E>,
{
RemoveCodeBuilder::new(self, caller, code_hash)
}
fn call<'a, Args: Sync + Encode + Clone, RetType: Send + Decode>(
&'a mut self,
caller: &'a Keypair,
message: &'a CallBuilderFinal<E, Args, RetType>,
) -> CallBuilder<'a, E, Args, RetType, Self>
where
Self: Sized + BuilderClient<E>,
{
CallBuilder::new(self, caller, message)
}
}
#[async_trait]
pub trait BuilderClient<E: Environment>: ContractsBackend<E> {
async fn bare_call<Args: Sync + Encode + Clone, RetType: Send + Decode>(
&mut self,
caller: &Keypair,
message: &CallBuilderFinal<E, Args, RetType>,
value: E::Balance,
gas_limit: Weight,
storage_deposit_limit: Option<E::Balance>,
) -> Result<Self::EventLog, Self::Error>
where
CallBuilderFinal<E, Args, RetType>: Clone;
async fn bare_call_dry_run<Args: Sync + Encode + Clone, RetType: Send + Decode>(
&mut self,
caller: &Keypair,
message: &CallBuilderFinal<E, Args, RetType>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<CallDryRunResult<E, RetType>, Self::Error>
where
CallBuilderFinal<E, Args, RetType>: Clone;
async fn bare_upload(
&mut self,
contract_name: &str,
caller: &Keypair,
storage_deposit_limit: Option<E::Balance>,
) -> Result<UploadResult<E, Self::EventLog>, Self::Error>;
async fn bare_remove_code(
&mut self,
caller: &Keypair,
code_hash: E::Hash,
) -> Result<Self::EventLog, Self::Error>;
async fn bare_instantiate<Contract: Clone, Args: Send + Sync + Encode + Clone, R>(
&mut self,
contract_name: &str,
caller: &Keypair,
constructor: &mut CreateBuilderPartial<E, Contract, Args, R>,
value: E::Balance,
gas_limit: Weight,
storage_deposit_limit: Option<E::Balance>,
) -> Result<BareInstantiationResult<E, Self::EventLog>, Self::Error>;
async fn bare_instantiate_dry_run<
Contract: Clone,
Args: Send + Sync + Encode + Clone,
R,
>(
&mut self,
contract_name: &str,
caller: &Keypair,
constructor: &mut CreateBuilderPartial<E, Contract, Args, R>,
value: E::Balance,
storage_deposit_limit: Option<E::Balance>,
) -> Result<InstantiateDryRunResult<E>, Self::Error>;
}