git.delta.rocks / unique-network / refs/commits / 735c2a469e47

difftreelog

Cleanup chain extension

Greg Zaitsev2021-01-28parent: #f00271a.patch.diff
in: master

2 files changed

modifiedruntime/src/chain_extension.rsdiffbeforeafterboth
after · runtime/src/chain_extension.rs
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;3637impl ChainExtension for NFTExtension {38    fn call<E: Ext>(func_id: u32, env: Environment<E, InitState>) -> Result<RetVal, DispatchError>39    where40        <E::T as SysConfig>::AccountId: UncheckedFrom<<E::T as SysConfig>::Hash> + AsRef<[u8]>,41    {42        // The memory of the vm stores buf in scale-codec43        match func_id {44            0 => {45                let mut env = env.buf_in_buf_out();46                let input: NFTExtTransfer<E> = env.read_as()?;4748                // Sender to AccountId3249                let mut bytesSender: [u8; 32] = [0; 32];50                let addrVecSender: Vec<u8> = env.ext().caller().encode();51                for i in 0..32 {52                    bytesSender[i] = addrVecSender[i];53                }54                let sender = AccountId32::from(bytesSender);5556                // Recipient to AccountId3257                let mut bytesRec: [u8; 32] = [0; 32];58                let addrVecRec: Vec<u8> = input.recipient.encode();59                for i in 0..32 {60                    bytesRec[i] = addrVecRec[i];61                }62                let recipient = AccountId32::from(bytesRec);6364                match pallet_nft::Module::<Runtime>::transfer_internal(sender, recipient, input.collection_id, input.token_id, input.amount) {65                    Ok(_) => Ok(RetVal::Converging(func_id)),66                    DispatchError => Err(DispatchError::Other("Transfer error"))67                }68            },69			_ => {70				panic!("Passed unknown func_id to test chain extension: {}", func_id);71            }72        }73    }74}75
modifiedsmart_contracs/transfer/Cargo.tomldiffbeforeafterboth
--- a/smart_contracs/transfer/Cargo.toml
+++ b/smart_contracs/transfer/Cargo.toml
@@ -5,11 +5,11 @@
 edition = "2018"
 
 [dependencies]
-ink_primitives = { git = "https://github.com/paritytech/ink", branch = "master", default-features = false }
-ink_metadata = { git = "https://github.com/paritytech/ink", branch = "master", default-features = false, features = ["derive"], optional = true }
-ink_env = { git = "https://github.com/paritytech/ink", branch = "master", default-features = false }
-ink_storage = { git = "https://github.com/paritytech/ink", branch = "master", default-features = false }
-ink_lang = { git = "https://github.com/paritytech/ink", branch = "master", default-features = false }
+ink_primitives = { git = "https://github.com/usetech-llc/ink", branch = "unique", default-features = false }
+ink_metadata = { git = "https://github.com/usetech-llc/ink", branch = "unique", default-features = false, features = ["derive"], optional = true }
+ink_env = { git = "https://github.com/usetech-llc/ink", branch = "unique", default-features = false }
+ink_storage = { git = "https://github.com/usetech-llc/ink", branch = "unique", default-features = false }
+ink_lang = { git = "https://github.com/usetech-llc/ink", branch = "unique", default-features = false }
 
 scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
 scale-info = { version = "0.4.1", default-features = false, features = ["derive"], optional = true }