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

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::*;20use nft_data_structs::*;2122/// Create item parameters23#[derive(Debug, PartialEq, Encode, Decode)]24pub struct NFTExtCreateItem<E: Ext> {25    pub owner: <E::T as SysConfig>::AccountId,26    pub collection_id: u32,27    pub data: CreateItemData,28}2930/// Transfer parameters31#[derive(Debug, PartialEq, Encode, Decode)]32pub struct NFTExtTransfer<E: Ext> {33    pub recipient: <E::T as SysConfig>::AccountId,34    pub collection_id: u32,35    pub token_id: u32,36    pub amount: u128,37}3839/// The chain Extension of NFT pallet40pub struct NFTExtension;4142impl<C: Config + pallet_contracts::Config> ChainExtension<C> for NFTExtension {43    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>44    where45        E: Ext<T = C>,46        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,47    {48        // The memory of the vm stores buf in scale-codec49        match func_id {50            0 => {51                let mut env = env.buf_in_buf_out();52                let input: NFTExtTransfer<E> = env.read_as()?;5354                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;5556                match pallet_nft::Module::<C>::transfer_internal(57                    env.ext().caller().clone(),58                    input.recipient,59                    &collection,60                    input.token_id,61                    input.amount,62                ) {63                    Ok(_) => Ok(RetVal::Converging(func_id)),64                    _ => Err(DispatchError::Other("Transfer error"))65                }66            },67            1 => {68                // Create Item69                let mut env = env.buf_in_buf_out();70                let input: NFTExtCreateItem<E> = env.read_as()?;7172                match pallet_nft::Module::<C>::create_item_internal(73                    env.ext().address().clone(),74                    input.collection_id,75                    input.owner,76                    input.data,77                ) {78                    Ok(_) => Ok(RetVal::Converging(func_id)),79                    _ => Err(DispatchError::Other("CreateItem error"))80                }81            },82            _ => {83                panic!("Passed unknown func_id to test chain extension: {}", func_id);84            }85        }86    }87}