git.delta.rocks / unique-network / refs/commits / eb5b53586bb9

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;22use sp_runtime::AccountId32;23use crate::Vec;2425/// Create item parameters26#[derive(Debug, PartialEq, Encode, Decode)]27pub struct NFTExtCreateItem<E: Ext> {28    pub owner: <E::T as SysConfig>::AccountId,29    pub collection_id: u32,30    pub data: CreateItemData,31}3233/// Transfer parameters34#[derive(Debug, PartialEq, Encode, Decode)]35pub struct NFTExtTransfer<E: Ext> {36    pub recipient: <E::T as SysConfig>::AccountId,37    pub collection_id: u32,38    pub token_id: u32,39    pub amount: u128,40}4142/// The chain Extension of NFT pallet43pub struct NFTExtension;4445impl<C: Config + pallet_contracts::Config> ChainExtension<C> for NFTExtension {46    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>47    where48        E: Ext<T = C>,49        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,50    {51        // The memory of the vm stores buf in scale-codec52        match func_id {53            0 => {54                let mut env = env.buf_in_buf_out();55                let input: NFTExtTransfer<E> = env.read_as()?;5657                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;5859                match pallet_nft::Module::<C>::transfer_internal(60                    env.ext().caller().clone(),61                    input.recipient,62                    &collection,63                    input.token_id,64                    input.amount,65                ) {66                    Ok(_) => Ok(RetVal::Converging(func_id)),67                    _ => Err(DispatchError::Other("Transfer error"))68                }69            },70            1 => {71                // Create Item72                let mut env = env.buf_in_buf_out();73                let input: NFTExtCreateItem<E> = env.read_as()?;7475                match pallet_nft::Module::<C>::create_item_internal(76                    env.ext().address().clone(),77                    input.collection_id,78                    input.owner,79                    input.data,80                ) {81                    Ok(_) => Ok(RetVal::Converging(func_id)),82                    _ => Err(DispatchError::Other("CreateItem error"))83                }84            },85            _ => {86                panic!("Passed unknown func_id to test chain extension: {}", func_id);87            }88        }89    }90}