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

difftreelog

source

runtime/src/chain_extension.rs2.7 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::*;2021/// Create item parameters22#[derive(Debug, PartialEq, Encode, Decode)]23pub struct NFTExtCreateItem<E: Ext> {24    pub owner: <E::T as SysConfig>::AccountId,25    pub collection_id: u32,26    pub data: CreateItemData,27}2829/// Transfer parameters30#[derive(Debug, PartialEq, Encode, Decode)]31pub struct NFTExtTransfer<E: Ext> {32    pub recipient: <E::T as SysConfig>::AccountId,33    pub collection_id: u32,34    pub token_id: u32,35    pub amount: u128,36}3738/// The chain Extension of NFT pallet39pub struct NFTExtension;4041impl<C: Config> ChainExtension<C> for NFTExtension {42    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>43    where44        E: Ext<T = C>,45        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,46    {47        // The memory of the vm stores buf in scale-codec48        match func_id {49            0 => {50                let mut env = env.buf_in_buf_out();51                let input: NFTExtTransfer<E> = env.read_as()?;5253                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;5455                match pallet_nft::Module::<C>::transfer_internal(56                    env.ext().caller().clone(),57                    input.recipient,58                    &collection,59                    input.token_id,60                    input.amount,61                ) {62                    Ok(_) => Ok(RetVal::Converging(func_id)),63                    _ => Err(DispatchError::Other("Transfer error"))64                }65            },66            1 => {67                // Create Item68                let mut env = env.buf_in_buf_out();69                let input: NFTExtCreateItem<E> = env.read_as()?;7071                match pallet_nft::Module::<C>::create_item_internal(72                    env.ext().address().clone(),73                    input.collection_id,74                    input.owner,75                    input.data,76                ) {77                    Ok(_) => Ok(RetVal::Converging(func_id)),78                    _ => Err(DispatchError::Other("CreateItem error"))79                }80            },81            _ => {82                panic!("Passed unknown func_id to test chain extension: {}", func_id);83            }84        }85    }86}