git.delta.rocks / unique-network / refs/commits / 2f9c8d434e2f

difftreelog

source

pallets/unique/src/eth/sponsoring.rs3.9 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 pallet_evm::account::CrossAccountId;2829use pallet_nonfungible::erc::{30	UniqueNFTCall, ERC721UniqueExtensionsCall, ERC721MintableCall, ERC721Call,31};32use pallet_fungible::erc::{UniqueFungibleCall, ERC20Call};33use up_data_structs::{TokenId, CreateItemData, CreateNftData};3435pub struct UniqueEthSponsorshipHandler<T: Config>(PhantomData<*const T>);36impl<T: Config> SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>)>37	for UniqueEthSponsorshipHandler<T>38{39	fn get_sponsor(who: &T::CrossAccountId, call: &(H160, Vec<u8>)) -> Option<T::CrossAccountId> {40		let collection_id = map_eth_to_id(&call.0)?;41		let collection = <CollectionHandle<T>>::new(collection_id)?;42		let sponsor = collection.sponsorship.sponsor()?.clone();43		let (method_id, mut reader) = AbiReader::new_call(&call.1).ok()?;44		Some(T::CrossAccountId::from_sub(match &collection.mode {45			crate::CollectionMode::NFT => {46				let call = <UniqueNFTCall<T>>::parse(method_id, &mut reader).ok()??;47				match call {48					UniqueNFTCall::ERC721UniqueExtensions(49						ERC721UniqueExtensionsCall::Transfer { token_id, .. },50					) => {51						let token_id: TokenId = token_id.try_into().ok()?;52						withdraw_transfer::<T>(&collection, &who, &token_id).map(|()| sponsor)53					}54					UniqueNFTCall::ERC721Mintable(55						ERC721MintableCall::Mint { token_id, .. }56						| ERC721MintableCall::MintWithTokenUri { token_id, .. },57					) => {58						let _token_id: TokenId = token_id.try_into().ok()?;59						withdraw_create_item::<T>(60							&collection,61							&who,62							&CreateItemData::NFT(CreateNftData::default()),63						)64						.map(|()| sponsor)65					}66					UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, from, .. }) => {67						let token_id: TokenId = token_id.try_into().ok()?;68						let from = T::CrossAccountId::from_eth(from);69						withdraw_transfer::<T>(&collection, &from, &token_id).map(|()| sponsor)70					}71					UniqueNFTCall::ERC721(ERC721Call::Approve { token_id, .. }) => {72						let token_id: TokenId = token_id.try_into().ok()?;73						withdraw_approve::<T>(&collection, who.as_sub(), &token_id)74							.map(|()| sponsor)75					}76					_ => None,77				}78			}79			crate::CollectionMode::Fungible(_) => {80				let call = <UniqueFungibleCall<T>>::parse(method_id, &mut reader).ok()??;81				#[allow(clippy::single_match)]82				match call {83					UniqueFungibleCall::ERC20(ERC20Call::Transfer { .. }) => {84						withdraw_transfer::<T>(&collection, who, &TokenId::default())85							.map(|()| sponsor)86					}87					UniqueFungibleCall::ERC20(ERC20Call::TransferFrom { from, .. }) => {88						let from = T::CrossAccountId::from_eth(from);89						withdraw_transfer::<T>(&collection, &from, &TokenId::default())90							.map(|()| sponsor)91					}92					UniqueFungibleCall::ERC20(ERC20Call::Approve { .. }) => {93						withdraw_approve::<T>(&collection, who.as_sub(), &TokenId::default())94							.map(|()| sponsor)95					}96					_ => None,97				}98			}99			_ => None,100		}?))101	}102}