git.delta.rocks / unique-network / refs/commits / 3ab9e10ac5b6

difftreelog

source

pallets/unique/src/eth/sponsoring.rs3.6 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! Implements EVM sponsoring logic via OnChargeEVMTransaction1819use crate::{Config, sponsorship::*};20use evm_coder::{Call, abi::AbiReader};21use pallet_common::{CollectionHandle, eth::map_eth_to_id};22use sp_core::H160;23use sp_std::prelude::*;24use up_sponsorship::SponsorshipHandler;25use core::marker::PhantomData;26use core::convert::TryInto;27use up_data_structs::TokenId;28use up_evm_mapping::EvmBackwardsAddressMapping;29use pallet_common::account::CrossAccountId;3031use pallet_nonfungible::erc::{UniqueNFTCall, ERC721UniqueExtensionsCall, ERC721Call};32use pallet_fungible::erc::{UniqueFungibleCall, ERC20Call};3334pub struct UniqueEthSponsorshipHandler<T: Config>(PhantomData<*const T>);35impl<T: Config> SponsorshipHandler<T::AccountId, (H160, Vec<u8>)> for UniqueEthSponsorshipHandler<T> {36	fn get_sponsor(who: &T::AccountId, call: &(H160, Vec<u8>)) -> Option<T::AccountId> {37		let collection_id = map_eth_to_id(&call.0)?;38		let collection = <CollectionHandle<T>>::new(collection_id)?;39		let sponsor = collection.sponsorship.sponsor()?.clone();40		let who = T::CrossAccountId::from_sub(who.clone());41		let (method_id, mut reader) = AbiReader::new_call(&call.1).ok()?;42		match &collection.mode {43			crate::CollectionMode::NFT => {44				let call = <UniqueNFTCall<T>>::parse(method_id, &mut reader).ok()??;45				match call {46					UniqueNFTCall::ERC721UniqueExtensions(47						ERC721UniqueExtensionsCall::Transfer { token_id, .. },48					) => {49						let token_id: TokenId = token_id.try_into().ok()?;50						withdraw_transfer::<T>(&collection, &who, &token_id).map(|()| sponsor)51					}52					UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, from, .. }) => {53						let token_id: TokenId = token_id.try_into().ok()?;54						let from = T::CrossAccountId::from_eth(from);55						withdraw_transfer::<T>(&collection, &from, &token_id).map(|()| sponsor)56					}57					UniqueNFTCall::ERC721(ERC721Call::Approve { token_id, .. }) => {58						let token_id: TokenId = token_id.try_into().ok()?;59						withdraw_approve::<T>(&collection, who.as_sub(), &token_id)60							.map(|()| sponsor)61					}62					_ => None,63				}64			}65			crate::CollectionMode::Fungible(_) => {66				let call = <UniqueFungibleCall<T>>::parse(method_id, &mut reader).ok()??;67				#[allow(clippy::single_match)]68				match call {69					UniqueFungibleCall::ERC20(ERC20Call::Transfer { .. }) => {70						withdraw_transfer::<T>(&collection, &who, &TokenId::default())71							.map(|()| sponsor)72					}73					UniqueFungibleCall::ERC20(ERC20Call::TransferFrom { from, .. }) => {74						let from = T::CrossAccountId::from_eth(from);75						withdraw_transfer::<T>(&collection, &from, &TokenId::default())76							.map(|()| sponsor)77					}78					UniqueFungibleCall::ERC20(ERC20Call::Approve { .. }) => {79						withdraw_approve::<T>(&collection, who.as_sub(), &TokenId::default())80							.map(|()| sponsor)81					}82					_ => None,83				}84			}85			_ => None,86		}87	}88}