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

difftreelog

source

runtime/src/chain_extension.rs3.6 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 crate::Vec;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#[derive(Debug, PartialEq, Encode, Decode)]40pub struct NFTExtCreateMultipleItems<E: Ext> {41    pub owner: <E::T as SysConfig>::AccountId,42    pub collection_id: u32,43    pub data: Vec<CreateItemData>,44}4546/// The chain Extension of NFT pallet47pub struct NFTExtension;4849impl<C: Config> ChainExtension<C> for NFTExtension {50    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>51    where52        E: Ext<T = C>,53        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,54    {55        // The memory of the vm stores buf in scale-codec56        match func_id {57            0 => {58                let mut env = env.buf_in_buf_out();59                let input: NFTExtTransfer<E> = env.read_as()?;6061                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;6263                match pallet_nft::Module::<C>::transfer_internal(64                    env.ext().caller().clone(),65                    input.recipient,66                    &collection,67                    input.token_id,68                    input.amount,69                ) {70                    Ok(_) => Ok(RetVal::Converging(func_id)),71                    _ => Err(DispatchError::Other("Transfer error"))72                }73            },74            1 => {75                // Create Item76                let mut env = env.buf_in_buf_out();77                let input: NFTExtCreateItem<E> = env.read_as()?;7879                match pallet_nft::Module::<C>::create_item_internal(80                    env.ext().address().clone(),81                    input.collection_id,82                    input.owner,83                    input.data,84                ) {85                    Ok(_) => Ok(RetVal::Converging(func_id)),86                    _ => Err(DispatchError::Other("CreateItem error"))87                }88            },89            2 => {90                // Create multiple items91                let mut env = env.buf_in_buf_out();92                let input: NFTExtCreateMultipleItems<E> = env.read_as()?;9394                let collection = pallet_nft::Module::<C>::get_collection(input.collection_id)?;9596                match pallet_nft::Module::<C>::create_multiple_items_internal(97                    env.ext().address().clone(),98                    &collection,99                    input.owner,100                    input.data,101                ) {102                    Ok(_) => Ok(RetVal::Converging(func_id)),103                    _ => Err(DispatchError::Other("CreateMultipleItems error"))104                }105            },106            _ => {107                panic!("Passed unknown func_id to test chain extension: {}", func_id);108            }109        }110    }111}