git.delta.rocks / unique-network / refs/commits / 397f5bd1733f

difftreelog

refactor remove unnecessary reencoding in CE

Yaroslav Bolyukin2021-05-05parent: #d6ccebb.patch.diff
in: master

1 file changed

modifiedruntime/src/chain_extension.rsdiffbeforeafterboth
before · runtime/src/chain_extension.rs
1//! 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::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,48    {49        // The memory of the vm stores buf in scale-codec50        match func_id {51            0 => {52                let mut env = env.buf_in_buf_out();53                let input: NFTExtTransfer<E> = env.read_as()?;5455                // Sender to AccountId3256                let mut bytes_sender: [u8; 32] = [0; 32];57                let addr_vec_sender: Vec<u8> = env.ext().caller().encode();58                for i in 0..32 {59                    bytes_sender[i] = addr_vec_sender[i];60                }61                let sender = AccountId32::from(bytes_sender);6263                // Recipient to AccountId3264                let mut bytes_rec: [u8; 32] = [0; 32];65                let addr_vec_rec: Vec<u8> = input.recipient.encode();66                for i in 0..32 {67                    bytes_rec[i] = addr_vec_rec[i];68                }69                let recipient = AccountId32::from(bytes_rec);7071                let collection = pallet_nft::Module::<Runtime>::get_collection(input.collection_id)?;7273                match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, &collection, input.token_id, input.amount) {74                    Ok(_) => Ok(RetVal::Converging(func_id)),75                    _ => Err(DispatchError::Other("Transfer error"))76                }77            },78            1 => {79                // Create Item80                let mut env = env.buf_in_buf_out();81                let input: NFTExtCreateItem<E> = env.read_as()?;8283                // Contract address to AccountId3284                let mut bytes_contract: [u8; 32] = [0; 32];85                let addr_vec_contract: Vec<u8> = env.ext().address().encode();86                for i in 0..32 {87                    bytes_contract[i] = addr_vec_contract[i];88                }89                let sender = AccountId32::from(bytes_contract);9091                // Recipient to AccountId3292                let mut bytes_rec: [u8; 32] = [0; 32];93                let addr_vec_rec: Vec<u8> = input.owner.encode();94                for i in 0..32 {95                    bytes_rec[i] = addr_vec_rec[i];96                }97                let recipient = AccountId32::from(bytes_rec);9899                // Parse data100                let cdata = input.data.encode();101                let vdata: [u8; 0] = [0; 0];102                let item_data = CreateItemData::NFT(CreateNftData{103                    const_data: cdata,104                    variable_data: vdata.to_vec()105                });106107                match pallet_nft::Module::<Runtime>::create_item_internal(sender, input.collection_id, recipient, item_data) {108                    Ok(_) => Ok(RetVal::Converging(func_id)),109                    _ => Err(DispatchError::Other("CreateItem error"))110                }111            },112            _ => {113                panic!("Passed unknown func_id to test chain extension: {}", func_id);114            }115        }116    }117}
after · runtime/src/chain_extension.rs
1//! 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}