git.delta.rocks / unique-network / refs/commits / 75e888b5af30

difftreelog

source

runtime/src/chain_extension.rs2.8 KiBsourcehistory
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 nft_data_structs::*;21// use crate::Runtime;2223use sp_runtime::AccountId32;24use crate::Vec;2526/// Create item parameters27#[derive(Debug, PartialEq, Encode, Decode)]28pub struct NFTExtCreateItem<E: Ext> {29    pub owner: <E::T as SysConfig>::AccountId,30    pub collection_id: u32,31    pub data: CreateItemData,32}3334/// Transfer parameters35#[derive(Debug, PartialEq, Encode, Decode)]36pub struct NFTExtTransfer<E: Ext> {37    pub recipient: <E::T as SysConfig>::AccountId,38    pub collection_id: u32,39    pub token_id: u32,40    pub amount: u128,41}4243/// The chain Extension of NFT pallet44pub struct NFTExtension;4546impl<C: Config + pallet_contracts::Config> ChainExtension<C> for NFTExtension {47    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>48    where49        E: Ext<T = C>,50        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,51    {52        // The memory of the vm stores buf in scale-codec53        match func_id {54            0 => {55                let mut env = env.buf_in_buf_out();56                let input: NFTExtTransfer<E> = env.read_as()?;5758                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;5960                match pallet_nft::Module::<C>::transfer_internal(61                    env.ext().caller().clone(),62                    input.recipient,63                    &collection,64                    input.token_id,65                    input.amount,66                ) {67                    Ok(_) => Ok(RetVal::Converging(func_id)),68                    _ => Err(DispatchError::Other("Transfer error"))69                }70            },71            1 => {72                // Create Item73                let mut env = env.buf_in_buf_out();74                let input: NFTExtCreateItem<E> = env.read_as()?;7576                match pallet_nft::Module::<C>::create_item_internal(77                    env.ext().address().clone(),78                    input.collection_id,79                    input.owner,80                    input.data,81                ) {82                    Ok(_) => Ok(RetVal::Converging(func_id)),83                    _ => Err(DispatchError::Other("CreateItem error"))84                }85            },86            _ => {87                panic!("Passed unknown func_id to test chain extension: {}", func_id);88            }89        }90    }91}