substrate-api-client icon indicating copy to clipboard operation
substrate-api-client copied to clipboard

Extend pallet_contract_tests

Open haerdib opened this issue 1 year ago • 0 comments

PR #737 updates the pallet contracts, but some calls are not yet tested. This needs to be updated.

The old example code does not work anymore as is (see below). The contract was not accepted as valid by the kitchensink node. PR #737 includes a .wasm file built with according to this tutorial: https://docs.substrate.io/tutorials/smart-contracts/prepare-your-first-contract/ This contract is accepted, but it gets reverted immediately (the same happens when uploaded via polkadot.js, so the problem is not the api-code). I did not find out why. But that at least means, that everything regarding code upload and instantiating is working. Calls and other stuff is not yet tested and needs to be updated.

Old example code:

#[allow(unused)]
#[derive(Decode)]
struct ContractInstantiatedEventArgs {
	deployer: AccountId,
	contract: AccountId,
}

impl StaticEvent for ContractInstantiatedEventArgs {
	const PALLET: &'static str = "Contracts";
	const EVENT: &'static str = "Instantiated";
}

#[tokio::main]
async fn main() {
	env_logger::init();

	// Initialize api and set the signer (sender) that is used to sign the extrinsics.
	let signer = AccountKeyring::Alice.pair();
	let client = JsonrpseeClient::with_default_url().await.unwrap();
	let mut api = Api::<AssetRuntimeConfig, _>::new(client).await.unwrap();
	api.set_signer(signer.into());

	println!("[+] Alice's Account Nonce is {}", api.get_nonce().await.unwrap());

	// contract to be deployed on the chain
	const CONTRACT: &str = r#"
(module
    (func (export "call"))
    (func (export "deploy"))
)
"#;
	let wasm = wabt::wat2wasm(CONTRACT).expect("invalid wabt");

	let xt = api
		.contract_instantiate_with_code(1_000_000_000_000_000, 500_000, wasm, vec![1u8], vec![1u8])
		.await
		.unwrap();

	println!("[+] Creating a contract instance with extrinsic:\n\n{:?}\n", xt);
	let report = api.submit_and_watch_extrinsic_until(xt, XtStatus::InBlock).await.unwrap();
	println!("[+] Extrinsic is in Block. Hash: {:?}\n", report.block_hash.unwrap());

	println!("[+] Waiting for the contracts.Instantiated event");

	let associated_contract_events = report.events.unwrap();

	let contract_instantiated_events: Vec<ContractInstantiatedEventArgs> =
		associated_contract_events
			.iter()
			.filter_map(|event| event.as_event().unwrap())
			.collect();
	// We only expect one instantiated event
	assert_eq!(contract_instantiated_events.len(), 1);
	let contract = contract_instantiated_events[0].contract.clone();
	println!("[+] Event was received. Contract deployed at: {contract:?}\n");

	let xt = api.contract_call(contract.into(), 500_000, 500_000, vec![0u8]).await.unwrap();

	println!("[+] Calling the contract with extrinsic Extrinsic:\n{:?}\n\n", xt);
	let report = api.submit_and_watch_extrinsic_until(xt, XtStatus::Finalized).await.unwrap();
	println!("[+] Extrinsic got finalized. Extrinsic Hash: {:?}", report.extrinsic_hash);

haerdib avatar Feb 29 '24 13:02 haerdib