use ink_sandbox::{api::prelude::*, AccountIdFor, Sandbox};
use super::Session;
use crate::{
pallet_contracts::Config,
session::mock::ContractMock,
};
pub trait MockingApi<R: Config> {
fn deploy(&mut self, mock: ContractMock) -> AccountIdFor<R>;
fn mock_existing_contract(&mut self, _mock: ContractMock, _address: AccountIdFor<R>);
}
impl<T: Sandbox> MockingApi<T::Runtime> for Session<T>
where
T::Runtime: Config,
{
fn deploy(&mut self, mock: ContractMock) -> AccountIdFor<T::Runtime> {
let mock_bytes = wat::parse_str(DUMMY_CONTRACT).expect("Dummy contract should be valid");
let salt = self
.mocks
.lock()
.expect("Should be able to acquire lock on registry")
.salt();
let mock_address = self
.sandbox()
.deploy_contract(
mock_bytes,
0u32.into(),
vec![],
salt,
T::default_actor(),
T::default_gas_limit(),
None,
)
.result
.expect("Deployment of a dummy contract should succeed")
.account_id;
self.mocks
.lock()
.expect("Should be able to acquire lock on registry")
.register(mock_address.clone(), mock);
mock_address
}
fn mock_existing_contract(&mut self, _mock: ContractMock, _address: AccountIdFor<T::Runtime>) {
todo!("soon")
}
}
const DUMMY_CONTRACT: &str = r#"
(module
(import "env" "memory" (memory 1 1))
(func (export "deploy"))
(func (export "call") (unreachable))
)"#;