diff --git a/ownership-chain/precompile/laos-evolution/src/lib.rs b/ownership-chain/precompile/laos-evolution/src/lib.rs index 8c801c502..f508cf5d3 100644 --- a/ownership-chain/precompile/laos-evolution/src/lib.rs +++ b/ownership-chain/precompile/laos-evolution/src/lib.rs @@ -1,7 +1,7 @@ //! LAOS precompile module. #![cfg_attr(not(feature = "std"), no_std)] -use fp_evm::{Log, Precompile, PrecompileHandle, PrecompileOutput}; +use fp_evm::{Precompile, PrecompileHandle, PrecompileOutput}; use pallet_laos_evolution::{traits::LaosEvolution as LaosEvolutionT, Slot, TokenId}; use parity_scale_codec::Encode; use precompile_utils::{ @@ -9,7 +9,7 @@ use precompile_utils::{ FunctionModifier, LogExt, LogsBuilder, PrecompileHandleExt, }; -use sp_core::{H160, H256}; +use sp_core::H160; use sp_std::{fmt::Debug, marker::PhantomData, vec::Vec}; /// Solidity selector of the CreateCollection log, which is the Keccak of the Log signature. @@ -110,8 +110,10 @@ where let slot = input.read::()?; let to = input.read::
()?.0; let token_uri_raw = input.read::()?.0; - let token_uri = - token_uri_raw.try_into().map_err(|_| revert("invalid token uri length"))?; + let token_uri = token_uri_raw + .clone() + .try_into() + .map_err(|_| revert("invalid token uri length"))?; match LaosEvolution::mint_with_external_uri( caller.into(), @@ -121,21 +123,18 @@ where token_uri, ) { Ok(token_id) => { - let mut token_id_bytes = [0u8; 32]; - token_id.to_big_endian(&mut token_id_bytes); - - Log { - address: context.address, - topics: sp_std::vec![ - H256(SELECTOR_LOG_MINTED_WITH_EXTERNAL_TOKEN_URI), - H256::from(H160::zero()), - H256::from(to), - H256::from_low_u64_be(collection_id), - H256(token_id_bytes), - ], - data: Vec::new(), - } - .record(handle)?; + LogsBuilder::new(context.address) + .log2( + SELECTOR_LOG_MINTED_WITH_EXTERNAL_TOKEN_URI, + to, + EvmDataWriter::new() + .write(collection_id) + .write(slot) + .write(Bytes(token_uri_raw)) + .write(token_id) + .build(), + ) + .record(handle)?; Ok(succeed(EvmDataWriter::new().write(token_id).build())) }, diff --git a/ownership-chain/precompile/laos-evolution/src/tests.rs b/ownership-chain/precompile/laos-evolution/src/tests.rs index 9a4e45c79..e20ad6767 100644 --- a/ownership-chain/precompile/laos-evolution/src/tests.rs +++ b/ownership-chain/precompile/laos-evolution/src/tests.rs @@ -11,12 +11,14 @@ use precompile_utils::{ revert, succeed, testing::{create_mock_handle, create_mock_handle_from_input}, }; -use sp_core::{H160, U256}; +use sp_core::{H160, H256, U256}; use sp_std::vec::Vec; type AccountId = H160; type AddressMapping = pallet_evm::IdentityAddressMapping; +const ALICE: &str = "0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac"; + #[test] fn check_selectors() { assert_eq!(Action::CreateCollection as u32, 0x2069E953); @@ -83,7 +85,7 @@ fn create_collection_should_generate_log() { impl_precompile_mock_simple!(Mock, Ok(123), None, Ok(0.into()), None); let input = EvmDataWriter::new_with_selector(Action::CreateCollection) - .write(Address(H160([1u8; 20]))) + .write(Address(H160::from_str(ALICE).unwrap())) .build(); let mut handle = create_mock_handle_from_input(input); @@ -96,7 +98,7 @@ fn create_collection_should_generate_log() { assert_eq!(logs[0].topics[0], SELECTOR_LOG_NEW_COLLECTION.into()); assert_eq!( logs[0].topics[1], - H256::from_str("0x0000000000000000000000000101010101010101010101010101010101010101") + H256::from_str("0x000000000000000000000000f24ff3a9cf04c71dbc94d0b566f7a27b94566cac") .unwrap() ); assert_eq!( @@ -108,6 +110,54 @@ fn create_collection_should_generate_log() { ); } +#[test] +fn mint_with_external_uri_should_generate_log() { + impl_precompile_mock_simple!( + Mock, + Ok(0), + None, + Ok(U256::from_str("0x010203").unwrap()), // return token id + None + ); + + let input = EvmDataWriter::new_with_selector(Action::Mint) + .write(U256::from(123)) // collection_id + .write(U256::from(9)) // slot + .write(Address(H160::from_str(ALICE).unwrap())) // to + .write(Bytes("ciao".into())) // token_uri + .build(); + let mut handle = create_mock_handle_from_input(input); + + let result = Mock::execute(&mut handle); + assert!(result.is_ok()); + let logs = handle.logs; + assert_eq!(logs.len(), 1); + assert_eq!(logs[0].address, H160::zero()); + assert_eq!(logs[0].topics.len(), 2); + assert_eq!(logs[0].topics[0], SELECTOR_LOG_MINTED_WITH_EXTERNAL_TOKEN_URI.into()); + assert_eq!( + logs[0].topics[1], + H256::from_str("0x000000000000000000000000f24ff3a9cf04c71dbc94d0b566f7a27b94566cac") + .unwrap() + ); + assert_eq!( + logs[0].data, + vec![ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 123, // collection id + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 9, // slot + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 1, 2, 3, // token id + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 4, // token uri length + 99, 105, 97, 111, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0 + ] // token uri + ); +} + #[test] fn create_collection_on_mock_with_nonzero_value_fails() { impl_precompile_mock_simple!(Mock, Ok(5), None, Ok(0.into()), None);