Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Mint with External URI Event and Adding Corresponding Tests #130

Merged
merged 11 commits into from
Oct 31, 2023
34 changes: 18 additions & 16 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 @@ -124,18 +126,18 @@ where
let mut token_id_bytes = [0u8; 32];
token_id.to_big_endian(&mut token_id_bytes);
asiniscalchi marked this conversation as resolved.
Show resolved Hide resolved

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))
asiniscalchi marked this conversation as resolved.
Show resolved Hide resolved
.write(token_id)
.build(),
)
.record(handle)?;

Ok(succeed(EvmDataWriter::new().write(token_id).build()))
},
Expand Down
50 changes: 49 additions & 1 deletion ownership-chain/precompile/laos-evolution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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;
Expand Down Expand Up @@ -108,6 +108,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([1u8; 20]))) // 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("0x0000000000000000000000000101010101010101010101010101010101010101")
magecnion marked this conversation as resolved.
Show resolved Hide resolved
.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