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

difftreelog

source

runtime/common/ethereum/sponsoring.rs4.7 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 TransactionValidityHack1819use core::{convert::TryInto, marker::PhantomData};20use evm_coder::{Call, abi::AbiReader};21use pallet_common::{CollectionHandle, eth::map_eth_to_id};22use pallet_evm::account::CrossAccountId;23use pallet_evm_transaction_payment::CallContext;24use pallet_nonfungible::{25	Config as NonfungibleConfig,26	erc::{27		UniqueNFTCall, ERC721UniqueExtensionsCall, ERC721MintableCall, ERC721Call,28		TokenPropertiesCall,29	},30};31use pallet_fungible::{32	Config as FungibleConfig,33	erc::{UniqueFungibleCall, ERC20Call},34};35use pallet_refungible::Config as RefungibleConfig;36use pallet_unique::Config as UniqueConfig;37use sp_std::prelude::*;38use up_data_structs::{CollectionMode, CreateItemData, CreateNftData, TokenId};39use up_sponsorship::SponsorshipHandler;4041use crate::{Runtime, runtime_common::sponsoring::*};4243pub type EvmSponsorshipHandler = (44	UniqueEthSponsorshipHandler<Runtime>,45	pallet_evm_contract_helpers::HelpersContractSponsoring<Runtime>,46);4748pub struct UniqueEthSponsorshipHandler<T: UniqueConfig>(PhantomData<*const T>);49impl<T: UniqueConfig + FungibleConfig + NonfungibleConfig + RefungibleConfig>50	SponsorshipHandler<T::CrossAccountId, CallContext> for UniqueEthSponsorshipHandler<T>51{52	fn get_sponsor(53		who: &T::CrossAccountId,54		call_context: &CallContext,55	) -> Option<T::CrossAccountId> {56		let collection_id = map_eth_to_id(&call_context.contract_address)?;57		let collection = <CollectionHandle<T>>::new(collection_id)?;58		let sponsor = collection.sponsorship.sponsor()?.clone();59		let (method_id, mut reader) = AbiReader::new_call(&call_context.input).ok()?;60		Some(T::CrossAccountId::from_sub(match &collection.mode {61			CollectionMode::NFT => {62				let call = <UniqueNFTCall<T>>::parse(method_id, &mut reader).ok()??;63				match call {64					UniqueNFTCall::TokenProperties(TokenPropertiesCall::SetProperty {65						token_id,66						key,67						value,68						..69					}) => {70						let token_id: TokenId = token_id.try_into().ok()?;71						withdraw_set_token_property::<T>(72							&collection,73							&who,74							&token_id,75							key.len() + value.len(),76						)77						.map(|()| sponsor)78					}79					UniqueNFTCall::ERC721UniqueExtensions(80						ERC721UniqueExtensionsCall::Transfer { token_id, .. },81					) => {82						let token_id: TokenId = token_id.try_into().ok()?;83						withdraw_transfer::<T>(&collection, &who, &token_id).map(|()| sponsor)84					}85					UniqueNFTCall::ERC721Mintable(86						ERC721MintableCall::Mint { token_id, .. }87						| ERC721MintableCall::MintWithTokenUri { token_id, .. },88					) => {89						let _token_id: TokenId = token_id.try_into().ok()?;90						withdraw_create_item::<T>(91							&collection,92							&who,93							&CreateItemData::NFT(CreateNftData::default()),94						)95						.map(|()| sponsor)96					}97					UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, from, .. }) => {98						let token_id: TokenId = token_id.try_into().ok()?;99						let from = T::CrossAccountId::from_eth(from);100						withdraw_transfer::<T>(&collection, &from, &token_id).map(|()| sponsor)101					}102					UniqueNFTCall::ERC721(ERC721Call::Approve { token_id, .. }) => {103						let token_id: TokenId = token_id.try_into().ok()?;104						withdraw_approve::<T>(&collection, who.as_sub(), &token_id)105							.map(|()| sponsor)106					}107					_ => None,108				}109			}110			CollectionMode::Fungible(_) => {111				let call = <UniqueFungibleCall<T>>::parse(method_id, &mut reader).ok()??;112				#[allow(clippy::single_match)]113				match call {114					UniqueFungibleCall::ERC20(ERC20Call::Transfer { .. }) => {115						withdraw_transfer::<T>(&collection, who, &TokenId::default())116							.map(|()| sponsor)117					}118					UniqueFungibleCall::ERC20(ERC20Call::TransferFrom { from, .. }) => {119						let from = T::CrossAccountId::from_eth(from);120						withdraw_transfer::<T>(&collection, &from, &TokenId::default())121							.map(|()| sponsor)122					}123					UniqueFungibleCall::ERC20(ERC20Call::Approve { .. }) => {124						withdraw_approve::<T>(&collection, who.as_sub(), &TokenId::default())125							.map(|()| sponsor)126					}127					_ => None,128				}129			}130			_ => None,131		}?))132	}133}