Skip to content

Commit

Permalink
Fixing Mint with External URI Event and Adding Corresponding Tests (#130
Browse files Browse the repository at this point in the history
)

* fix NewCollection event (#128)

* test + mint uses data field

* fmt

* refactoring test

* fmt

* removed merge line

* removed code

* unused cast

* back to Bytes

* using ALICE address in tests
  • Loading branch information
asiniscalchi authored Oct 31, 2023
1 parent a8f7dca commit aefc48b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
37 changes: 18 additions & 19 deletions ownership-chain/precompile/laos-evolution/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! 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::{
keccak256, revert, revert_dispatch_error, succeed, Address, Bytes, EvmDataWriter, EvmResult,
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.
Expand Down Expand Up @@ -110,8 +110,10 @@ where
let slot = input.read::<Slot>()?;
let to = input.read::<Address>()?.0;
let token_uri_raw = input.read::<Bytes>()?.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(),
Expand All @@ -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()))
},
Expand Down
56 changes: 53 additions & 3 deletions ownership-chain/precompile/laos-evolution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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!(
Expand All @@ -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);
Expand Down

0 comments on commit aefc48b

Please sign in to comment.