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

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