use anyhow::Result;
use subxt::{balances::Balances, contracts::*, system::System, ClientBuilder, DefaultNodeRuntime};
use crate::{ExtrinsicOpts, HexData};
pub(crate) fn execute_instantiate(
extrinsic_opts: &ExtrinsicOpts,
endowment: <DefaultNodeRuntime as Balances>::Balance,
gas_limit: u64,
code_hash: <DefaultNodeRuntime as System>::Hash,
data: HexData,
) -> Result<<DefaultNodeRuntime as System>::AccountId> {
async_std::task::block_on(async move {
let cli = ClientBuilder::<DefaultNodeRuntime>::new()
.set_url(&extrinsic_opts.url.to_string())
.build()
.await?;
let signer = extrinsic_opts.signer()?;
let events = cli
.instantiate_and_watch(&signer, endowment, gas_limit, &code_hash, &data.0)
.await?;
let instantiated = events
.instantiated()?
.ok_or(anyhow::anyhow!("Failed to find Instantiated event"))?;
Ok(instantiated.contract)
})
}
#[cfg(test)]
mod tests {
use std::{fs, io::Write};
use crate::{cmd::deploy::execute_deploy, util::tests::with_tmp_dir, ExtrinsicOpts, HexData};
use assert_matches::assert_matches;
const CONTRACT: &str = r#"
(module
(func (export "call"))
(func (export "deploy"))
)
"#;
#[test]
#[ignore] fn instantiate_contract() {
with_tmp_dir(|path| {
let wasm = wabt::wat2wasm(CONTRACT).expect("invalid wabt");
let wasm_path = path.join("test.wasm");
let mut file = fs::File::create(&wasm_path).unwrap();
let _ = file.write_all(&wasm);
let url = url::Url::parse("ws://localhost:9944").unwrap();
let extrinsic_opts = ExtrinsicOpts {
url,
suri: "//Alice".into(),
password: None,
};
let code_hash =
execute_deploy(&extrinsic_opts, Some(&wasm_path)).expect("Deploy should succeed");
let gas_limit = 500_000_000;
let result = super::execute_instantiate(
&extrinsic_opts,
100000000000000,
gas_limit,
code_hash,
HexData::default(),
);
assert_matches!(result, Ok(_));
Ok(())
})
}
}