git.delta.rocks / unique-network / refs/commits / 5900bfd2d2db

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