difftreelog
refactor remove unnecessary reencoding in CE
in: master
1 file changed
runtime/src/chain_extension.rsdiffbeforeafterboth1//! NFT Chain Extension23//4// This file is subject to the terms and conditions defined in5// file 'LICENSE', which is part of this source code package.6//78use codec::{Decode, Encode};910pub use pallet_contracts::chain_extension::RetVal;11use pallet_contracts::chain_extension::{12 ChainExtension, Environment, Ext, InitState, SysConfig, UncheckedFrom,13};1415pub use frame_support::debug;16use frame_support::dispatch::DispatchError;1718extern crate pallet_nft;19pub use pallet_nft::*;20use crate::Runtime;21use sp_runtime::AccountId32;22use crate::Vec;2324/// Create item parameters25#[derive(Debug, PartialEq, Encode, Decode)]26pub struct NFTExtCreateItem<E: Ext> {27 pub owner: <E::T as SysConfig>::AccountId,28 pub collection_id: u32,29 pub data: CreateItemData,30}3132/// Transfer parameters33#[derive(Debug, PartialEq, Encode, Decode)]34pub struct NFTExtTransfer<E: Ext> {35 pub recipient: <E::T as SysConfig>::AccountId,36 pub collection_id: u32,37 pub token_id: u32,38 pub amount: u128,39}4041/// The chain Extension of NFT pallet42pub struct NFTExtension;4344impl<C: Config> ChainExtension<C> for NFTExtension {45 fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>46 where47 E: Ext<T = C>,48 <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,49 {50 // The memory of the vm stores buf in scale-codec51 match func_id {52 0 => {53 let mut env = env.buf_in_buf_out();54 let input: NFTExtTransfer<E> = env.read_as()?;5556 let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;5758 match pallet_nft::Module::<C>::transfer_internal(59 env.ext().caller().clone(),60 input.recipient,61 &collection,62 input.token_id,63 input.amount,64 ) {65 Ok(_) => Ok(RetVal::Converging(func_id)),66 _ => Err(DispatchError::Other("Transfer error"))67 }68 },69 1 => {70 // Create Item71 let mut env = env.buf_in_buf_out();72 let input: NFTExtCreateItem<E> = env.read_as()?;7374 match pallet_nft::Module::<C>::create_item_internal(75 env.ext().address().clone(),76 input.collection_id,77 input.owner,78 input.data,79 ) {80 Ok(_) => Ok(RetVal::Converging(func_id)),81 _ => Err(DispatchError::Other("CreateItem error"))82 }83 },84 _ => {85 panic!("Passed unknown func_id to test chain extension: {}", func_id);86 }87 }88 }89}