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

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