12345678use 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;232425#[derive(Debug, PartialEq, Encode, Decode)]26pub struct NFTExtTransfer<E: Ext> {27 pub recipient: <E::T as SysConfig>::AccountId,28 pub collection_id: u32,29 pub token_id: u32,30 pub amount: u128,31}323334pub struct NFTExtension;3536impl ChainExtension for NFTExtension {37 fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>38 where39 <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,40 {41 42 match func_id {43 0 => {44 let mut env = env.buf_in_buf_out();45 let input: NFTExtTransfer<E> = env.read_as()?;4647 48 let mut bytes_sender: [u8; 32] = [0; 32];49 let addr_vec_sender: Vec<u8> = env.ext().caller().encode();50 for i in 0..32 {51 bytes_sender[i] = addr_vec_sender[i];52 }53 let sender = AccountId32::from(bytes_sender);5455 56 let mut bytes_rec: [u8; 32] = [0; 32];57 let addr_vec_rec: Vec<u8> = input.recipient.encode();58 for i in 0..32 {59 bytes_rec[i] = addr_vec_rec[i];60 }61 let recipient = AccountId32::from(bytes_rec);6263 match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, input.collection_id, input.token_id, input.amount) {64 Ok(_) => Ok(RetVal::Converging(func_id)),65 _ => Err(DispatchError::Other("Transfer error"))66 }67 },68 _ => {69 panic!("Passed unknown func_id to test chain extension: {}", func_id);70 }71 }72 }73}74