Skip to content

Commit

Permalink
Refactoring mint_with_external_uri function and adding tests (#129)
Browse files Browse the repository at this point in the history
* test + mint uses data field

* fmt

* refactoring test

* fmt
  • Loading branch information
asiniscalchi authored Oct 30, 2023
1 parent 9ebd1af commit 23e2d58
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
34 changes: 18 additions & 16 deletions evolution-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);

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
50 changes: 49 additions & 1 deletion evolution-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")
.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 23e2d58

Please sign in to comment.