From 397f5bd1733f6bdbd077262802008e0fa4aafc02 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Wed, 05 May 2021 16:10:00 +0000 Subject: [PATCH] refactor: remove unnecessary reencoding in CE --- --- a/runtime/src/chain_extension.rs +++ b/runtime/src/chain_extension.rs @@ -44,6 +44,7 @@ impl ChainExtension for NFTExtension { fn call(func_id: u32, env: Environment) -> Result where + E: Ext, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { // The memory of the vm stores buf in scale-codec @@ -52,25 +53,15 @@ let mut env = env.buf_in_buf_out(); let input: NFTExtTransfer = env.read_as()?; - // Sender to AccountId32 - let mut bytes_sender: [u8; 32] = [0; 32]; - let addr_vec_sender: Vec = env.ext().caller().encode(); - for i in 0..32 { - bytes_sender[i] = addr_vec_sender[i]; - } - let sender = AccountId32::from(bytes_sender); - - // Recipient to AccountId32 - let mut bytes_rec: [u8; 32] = [0; 32]; - let addr_vec_rec: Vec = input.recipient.encode(); - for i in 0..32 { - bytes_rec[i] = addr_vec_rec[i]; - } - let recipient = AccountId32::from(bytes_rec); + let collection = pallet_nft::Module::::get_collection(input.collection_id)?; - let collection = pallet_nft::Module::::get_collection(input.collection_id)?; - - match pallet_nft::Module::::transfer_internal(sender, recipient, &collection, input.token_id, input.amount) { + match pallet_nft::Module::::transfer_internal( + env.ext().caller().clone(), + input.recipient, + &collection, + input.token_id, + input.amount, + ) { Ok(_) => Ok(RetVal::Converging(func_id)), _ => Err(DispatchError::Other("Transfer error")) } @@ -80,31 +71,12 @@ let mut env = env.buf_in_buf_out(); let input: NFTExtCreateItem = env.read_as()?; - // Contract address to AccountId32 - let mut bytes_contract: [u8; 32] = [0; 32]; - let addr_vec_contract: Vec = env.ext().address().encode(); - for i in 0..32 { - bytes_contract[i] = addr_vec_contract[i]; - } - let sender = AccountId32::from(bytes_contract); - - // Recipient to AccountId32 - let mut bytes_rec: [u8; 32] = [0; 32]; - let addr_vec_rec: Vec = input.owner.encode(); - for i in 0..32 { - bytes_rec[i] = addr_vec_rec[i]; - } - let recipient = AccountId32::from(bytes_rec); - - // Parse data - let cdata = input.data.encode(); - let vdata: [u8; 0] = [0; 0]; - let item_data = CreateItemData::NFT(CreateNftData{ - const_data: cdata, - variable_data: vdata.to_vec() - }); - - match pallet_nft::Module::::create_item_internal(sender, input.collection_id, recipient, item_data) { + match pallet_nft::Module::::create_item_internal( + env.ext().address().clone(), + input.collection_id, + input.owner, + input.data, + ) { Ok(_) => Ok(RetVal::Converging(func_id)), _ => Err(DispatchError::Other("CreateItem error")) } -- gitstuff