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

difftreelog

source

runtime/common/ethereum/sponsoring.rs4.8 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_core::H160;38use sp_std::prelude::*;39use up_data_structs::{CollectionMode, CreateItemData, CreateNftData, TokenId};40use up_sponsorship::SponsorshipHandler;4142use crate::{Runtime, runtime_common::sponsoring::*};4344pub type EvmSponsorshipHandler = (45	UniqueEthSponsorshipHandler<Runtime>,46	pallet_evm_contract_helpers::HelpersContractSponsoring<Runtime>,47);4849pub struct UniqueEthSponsorshipHandler<T: UniqueConfig>(PhantomData<*const T>);50impl<T: UniqueConfig + FungibleConfig + NonfungibleConfig + RefungibleConfig>51	SponsorshipHandler<T::CrossAccountId, (H160, Vec<u8>), CallContext>52	for UniqueEthSponsorshipHandler<T>53{54	fn get_sponsor(55		who: &T::CrossAccountId,56		call: &(H160, Vec<u8>),57		_fee_limit: &CallContext,58	) -> Option<T::CrossAccountId> {59		let collection_id = map_eth_to_id(&call.0)?;60		let collection = <CollectionHandle<T>>::new(collection_id)?;61		let sponsor = collection.sponsorship.sponsor()?.clone();62		let (method_id, mut reader) = AbiReader::new_call(&call.1).ok()?;63		Some(T::CrossAccountId::from_sub(match &collection.mode {64			CollectionMode::NFT => {65				let call = <UniqueNFTCall<T>>::parse(method_id, &mut reader).ok()??;66				match call {67					UniqueNFTCall::TokenProperties(TokenPropertiesCall::SetProperty {68						token_id,69						key,70						value,71						..72					}) => {73						let token_id: TokenId = token_id.try_into().ok()?;74						withdraw_set_token_property::<T>(75							&collection,76							&who,77							&token_id,78							key.len() + value.len(),79						)80						.map(|()| sponsor)81					}82					UniqueNFTCall::ERC721UniqueExtensions(83						ERC721UniqueExtensionsCall::Transfer { token_id, .. },84					) => {85						let token_id: TokenId = token_id.try_into().ok()?;86						withdraw_transfer::<T>(&collection, &who, &token_id).map(|()| sponsor)87					}88					UniqueNFTCall::ERC721Mintable(89						ERC721MintableCall::Mint { token_id, .. }90						| ERC721MintableCall::MintWithTokenUri { token_id, .. },91					) => {92						let _token_id: TokenId = token_id.try_into().ok()?;93						withdraw_create_item::<T>(94							&collection,95							&who,96							&CreateItemData::NFT(CreateNftData::default()),97						)98						.map(|()| sponsor)99					}100					UniqueNFTCall::ERC721(ERC721Call::TransferFrom { token_id, from, .. }) => {101						let token_id: TokenId = token_id.try_into().ok()?;102						let from = T::CrossAccountId::from_eth(from);103						withdraw_transfer::<T>(&collection, &from, &token_id).map(|()| sponsor)104					}105					UniqueNFTCall::ERC721(ERC721Call::Approve { token_id, .. }) => {106						let token_id: TokenId = token_id.try_into().ok()?;107						withdraw_approve::<T>(&collection, who.as_sub(), &token_id)108							.map(|()| sponsor)109					}110					_ => None,111				}112			}113			CollectionMode::Fungible(_) => {114				let call = <UniqueFungibleCall<T>>::parse(method_id, &mut reader).ok()??;115				#[allow(clippy::single_match)]116				match call {117					UniqueFungibleCall::ERC20(ERC20Call::Transfer { .. }) => {118						withdraw_transfer::<T>(&collection, who, &TokenId::default())119							.map(|()| sponsor)120					}121					UniqueFungibleCall::ERC20(ERC20Call::TransferFrom { from, .. }) => {122						let from = T::CrossAccountId::from_eth(from);123						withdraw_transfer::<T>(&collection, &from, &TokenId::default())124							.map(|()| sponsor)125					}126					UniqueFungibleCall::ERC20(ERC20Call::Approve { .. }) => {127						withdraw_approve::<T>(&collection, who.as_sub(), &TokenId::default())128							.map(|()| sponsor)129					}130					_ => None,131				}132			}133			_ => None,134		}?))135	}136}