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

difftreelog

source

runtime/src/chain_extension.rs2.9 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::Runtime;21use sp_runtime::AccountId32;22use crate::Vec;23use frame_system::Origin;2425/// Transfer parameters26#[derive(Debug, PartialEq, Encode, Decode)]27pub struct NFTExtTransfer<E: Ext> {28    pub recipient: <E::T as SysConfig>::AccountId,29    pub collection_id: u32,30    pub token_id: u32,31    pub amount: u128,32}3334/// The chain Extension of NFT pallet35pub struct NFTExtension;3637// pub trait ToAccount32 {38//     fn to_account32<E: Ext>(addr: <E::T as SysConfig>::AccountId);39// }4041// impl ToAccount32 for NFTExtension {42//     fn to_account32<E: Ext>(addr: <E::T as SysConfig>::AccountId) 43//     where44//         <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,45//     {46//         let mut bytes: [u8; 32];47//         let addrVec: Vec<u8> = addr.encode();48//         for i in 0..32 {49//             bytes[i] = addrVec[i];50//         }51//         AccountId32::from(bytes)52//     }5354// }5556impl ChainExtension for NFTExtension {57    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>58    where59        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,60    {61        // The memory of the vm stores buf in scale-codec62        match func_id {63            0 => {64                let mut env = env.buf_in_buf_out();65                let input: NFTExtTransfer<E> = env.read_as()?;6667                // Sender to AccountId3268                let mut bytesSender: [u8; 32] = [0; 32];69                let addrVecSender: Vec<u8> = env.ext().caller().encode();70                for i in 0..32 {71                    bytesSender[i] = addrVecSender[i];72                }73                let sender = AccountId32::from(bytesSender);7475                // Recipient to AccountId3276                let mut bytesRec: [u8; 32] = [0; 32];77                let addrVecRec: Vec<u8> = input.recipient.encode();78                for i in 0..32 {79                    bytesRec[i] = addrVecRec[i];80                }81                let recipient = AccountId32::from(bytesRec);82838485                match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, input.collection_id, input.token_id, input.amount) {86                    Ok(_) => Ok(RetVal::Converging(func_id)),87                    DispatchError => Err(DispatchError::Other("Transfer error"))88                }8990                91            },92			_ => {93				panic!("Passed unknown func_id to test chain extension: {}", func_id);94            }95        }96    }97}98