#[cfg(test)] mod tests { use std::path::PathBuf; use dimension_engine_test_support::{ DeployItemBuilder, ExecuteRequestBuilder, InMemoryWasmTestBuilder, ARG_AMOUNT, DEFAULT_ACCOUNT_ADDR, DEFAULT_ACCOUNT_INITIAL_BALANCE, DEFAULT_GENESIS_CONFIG, DEFAULT_GENESIS_CONFIG_HASH, DEFAULT_PAYMENT, DEFAULT_RUN_GENESIS_REQUEST, }; use dimension_execution_engine::core::engine_state::{ run_genesis_request::RunGenesisRequest, GenesisAccount, }; use dimension_types::{ account::AccountHash, runtime_args, Key, Motes, PublicKey, RuntimeArgs, SecretKey, U512, }; const MY_ACCOUNT: [u8; 32] = [7u8; 32]; // Define `KEY` constant to match that in the contract. const KEY: &str = "my-key-name"; const VALUE: &str = "hello world"; const RUNTIME_ARG_NAME: &str = "message"; const CONTRACT_WASM: &str = "contract.wasm"; #[test] fn should_store_hello_world() { // Create keypair. let secret_key = SecretKey::ed25519_from_bytes(MY_ACCOUNT).unwrap(); let public_key = PublicKey::from(&secret_key); // Create an AccountHash from a public key. let account_addr = AccountHash::from(&public_key); // Create a GenesisAccount. let account = GenesisAccount::account( public_key, Motes::new(U512::from(DEFAULT_ACCOUNT_INITIAL_BALANCE)), None, ); let mut genesis_config = DEFAULT_GENESIS_CONFIG.clone(); genesis_config.ee_config_mut().push_account(account); let run_genesis_request = RunGenesisRequest::new( *DEFAULT_GENESIS_CONFIG_HASH, genesis_config.protocol_version(), genesis_config.take_ee_config(), ); // The test framework checks for compiled Wasm files in '/wasm'. Paths // relative to the current working dir (e.g. 'wasm/contract.wasm') can also be used, as can // absolute paths. let session_code = PathBuf::from(CONTRACT_WASM); let session_args = runtime_args! { RUNTIME_ARG_NAME => VALUE, }; let deploy_item = DeployItemBuilder::new() .with_empty_payment_bytes(runtime_args! { ARG_AMOUNT => *DEFAULT_PAYMENT }) .with_session_code(session_code, session_args) .with_authorization_keys(&[account_addr]) .with_address(account_addr) .build(); let execute_request = ExecuteRequestBuilder::from_deploy_item(deploy_item).build(); let mut builder = InMemoryWasmTestBuilder::default(); builder.run_genesis(&run_genesis_request).commit(); // prepare assertions. let result_of_query = builder.query( None, Key::Account(*DEFAULT_ACCOUNT_ADDR), &[KEY.to_string()], ); assert!(result_of_query.is_err()); // deploy the contract. builder.exec(execute_request).commit().expect_success(); // make assertions let result_of_query = builder .query(None, Key::Account(account_addr), &[KEY.to_string()]) .expect("should be stored value.") .as_cl_value() .expect("should be cl value.") .clone() .into_t::() .expect("should be string."); assert_eq!(result_of_query, VALUE); } #[test] fn should_error_on_missing_runtime_arg() { let secret_key = SecretKey::ed25519_from_bytes(MY_ACCOUNT).unwrap(); let public_key = PublicKey::from(&secret_key); let account_addr = AccountHash::from(&public_key); let session_code = PathBuf::from(CONTRACT_WASM); let session_args = RuntimeArgs::new(); let deploy_item = DeployItemBuilder::new() .with_empty_payment_bytes(runtime_args! {ARG_AMOUNT => *DEFAULT_PAYMENT}) .with_authorization_keys(&[account_addr]) .with_address(*DEFAULT_ACCOUNT_ADDR) .with_session_code(session_code, session_args) .build(); let execute_request = ExecuteRequestBuilder::from_deploy_item(deploy_item).build(); let mut builder = InMemoryWasmTestBuilder::default(); builder.run_genesis(&DEFAULT_RUN_GENESIS_REQUEST).commit(); builder.exec(execute_request).commit().expect_failure(); } } fn main() { panic!("Execute \"cargo test\" to test the contract, not \"cargo run\"."); }