git.delta.rocks / unique-network / refs/commits / 1d4e56c36dc5

difftreelog

source

pallets/fungible/src/erc.rs9.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//! ERC-20 standart support implementation.1819extern crate alloc;20use core::char::{REPLACEMENT_CHARACTER, decode_utf16};21use core::convert::TryInto;22use evm_coder::{23	abi::AbiType, ToLog, execution::*, generate_stubgen, solidity, solidity_interface, types::*,24	weight,25};26use up_data_structs::CollectionMode;27use pallet_common::erc::{CommonEvmHandler, PrecompileResult};28use sp_std::vec::Vec;29use pallet_evm::{account::CrossAccountId, PrecompileHandle};30use pallet_evm_coder_substrate::{call, dispatch_to_evm};31use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};32use pallet_common::{CollectionHandle, erc::CollectionCall};3334use crate::{35	Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,36	weights::WeightInfo,37};3839#[derive(ToLog)]40pub enum ERC20Events {41	Transfer {42		#[indexed]43		from: address,44		#[indexed]45		to: address,46		value: uint256,47	},48	Approval {49		#[indexed]50		owner: address,51		#[indexed]52		spender: address,53		value: uint256,54	},55}5657#[solidity_interface(name = ERC20, events(ERC20Events))]58impl<T: Config> FungibleHandle<T> {59	fn name(&self) -> Result<string> {60		Ok(decode_utf16(self.name.iter().copied())61			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))62			.collect::<string>())63	}64	fn symbol(&self) -> Result<string> {65		Ok(string::from_utf8_lossy(&self.token_prefix).into())66	}67	fn total_supply(&self) -> Result<uint256> {68		self.consume_store_reads(1)?;69		Ok(<TotalSupply<T>>::get(self.id).into())70	}7172	fn decimals(&self) -> Result<uint8> {73		Ok(if let CollectionMode::Fungible(decimals) = &self.mode {74			*decimals75		} else {76			unreachable!()77		})78	}79	fn balance_of(&self, owner: address) -> Result<uint256> {80		self.consume_store_reads(1)?;81		let owner = T::CrossAccountId::from_eth(owner);82		let balance = <Balance<T>>::get((self.id, owner));83		Ok(balance.into())84	}85	#[weight(<SelfWeightOf<T>>::transfer())]86	fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {87		let caller = T::CrossAccountId::from_eth(caller);88		let to = T::CrossAccountId::from_eth(to);89		let amount = amount.try_into().map_err(|_| "amount overflow")?;90		let budget = self91			.recorder92			.weight_calls_budget(<StructureWeight<T>>::find_parent());9394		<Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;95		Ok(true)96	}9798	#[weight(<SelfWeightOf<T>>::transfer_from())]99	fn transfer_from(100		&mut self,101		caller: caller,102		from: address,103		to: address,104		amount: uint256,105	) -> Result<bool> {106		let caller = T::CrossAccountId::from_eth(caller);107		let from = T::CrossAccountId::from_eth(from);108		let to = T::CrossAccountId::from_eth(to);109		let amount = amount.try_into().map_err(|_| "amount overflow")?;110		let budget = self111			.recorder112			.weight_calls_budget(<StructureWeight<T>>::find_parent());113114		<Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)115			.map_err(dispatch_to_evm::<T>)?;116		Ok(true)117	}118	#[weight(<SelfWeightOf<T>>::approve())]119	fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {120		let caller = T::CrossAccountId::from_eth(caller);121		let spender = T::CrossAccountId::from_eth(spender);122		let amount = amount.try_into().map_err(|_| "amount overflow")?;123124		<Pallet<T>>::set_allowance(self, &caller, &spender, amount)125			.map_err(dispatch_to_evm::<T>)?;126		Ok(true)127	}128	fn allowance(&self, owner: address, spender: address) -> Result<uint256> {129		self.consume_store_reads(1)?;130		let owner = T::CrossAccountId::from_eth(owner);131		let spender = T::CrossAccountId::from_eth(spender);132133		Ok(<Allowance<T>>::get((self.id, owner, spender)).into())134	}135}136137#[solidity_interface(name = ERC20Mintable)]138impl<T: Config> FungibleHandle<T> {139	/// Mint tokens for `to` account.140	/// @param to account that will receive minted tokens141	/// @param amount amount of tokens to mint142	#[weight(<SelfWeightOf<T>>::create_item())]143	fn mint(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {144		let caller = T::CrossAccountId::from_eth(caller);145		let to = T::CrossAccountId::from_eth(to);146		let amount = amount.try_into().map_err(|_| "amount overflow")?;147		let budget = self148			.recorder149			.weight_calls_budget(<StructureWeight<T>>::find_parent());150		<Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)151			.map_err(dispatch_to_evm::<T>)?;152		Ok(true)153	}154}155156#[solidity_interface(name = ERC20UniqueExtensions)]157impl<T: Config> FungibleHandle<T>158where159	T::AccountId: From<[u8; 32]>,160{161	/// @notice A description for the collection.162	fn description(&self) -> Result<string> {163		Ok(decode_utf16(self.description.iter().copied())164			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))165			.collect::<string>())166	}167168	#[weight(<SelfWeightOf<T>>::approve())]169	fn approve_cross(170		&mut self,171		caller: caller,172		spender: EthCrossAccount,173		amount: uint256,174	) -> Result<bool> {175		let caller = T::CrossAccountId::from_eth(caller);176		let spender = spender.into_sub_cross_account::<T>()?;177		let amount = amount.try_into().map_err(|_| "amount overflow")?;178179		<Pallet<T>>::set_allowance(self, &caller, &spender, amount)180			.map_err(dispatch_to_evm::<T>)?;181		Ok(true)182	}183184	/// Burn tokens from account185	/// @dev Function that burns an `amount` of the tokens of a given account,186	/// deducting from the sender's allowance for said account.187	/// @param from The account whose tokens will be burnt.188	/// @param amount The amount that will be burnt.189	#[solidity(hide)]190	#[weight(<SelfWeightOf<T>>::burn_from())]191	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {192		let caller = T::CrossAccountId::from_eth(caller);193		let from = T::CrossAccountId::from_eth(from);194		let amount = amount.try_into().map_err(|_| "amount overflow")?;195		let budget = self196			.recorder197			.weight_calls_budget(<StructureWeight<T>>::find_parent());198199		<Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)200			.map_err(dispatch_to_evm::<T>)?;201		Ok(true)202	}203204	/// Burn tokens from account205	/// @dev Function that burns an `amount` of the tokens of a given account,206	/// deducting from the sender's allowance for said account.207	/// @param from The account whose tokens will be burnt.208	/// @param amount The amount that will be burnt.209	#[weight(<SelfWeightOf<T>>::burn_from())]210	fn burn_from_cross(211		&mut self,212		caller: caller,213		from: EthCrossAccount,214		amount: uint256,215	) -> Result<bool> {216		let caller = T::CrossAccountId::from_eth(caller);217		let from = from.into_sub_cross_account::<T>()?;218		let amount = amount.try_into().map_err(|_| "amount overflow")?;219		let budget = self220			.recorder221			.weight_calls_budget(<StructureWeight<T>>::find_parent());222223		<Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)224			.map_err(dispatch_to_evm::<T>)?;225		Ok(true)226	}227228	/// Mint tokens for multiple accounts.229	/// @param amounts array of pairs of account address and amount230	#[weight(<SelfWeightOf<T>>::create_multiple_items_ex(amounts.len() as u32))]231	fn mint_bulk(&mut self, caller: caller, amounts: Vec<(address, uint256)>) -> Result<bool> {232		let caller = T::CrossAccountId::from_eth(caller);233		let budget = self234			.recorder235			.weight_calls_budget(<StructureWeight<T>>::find_parent());236		let amounts = amounts237			.into_iter()238			.map(|(to, amount)| {239				Ok((240					T::CrossAccountId::from_eth(to),241					amount.try_into().map_err(|_| "amount overflow")?,242				))243			})244			.collect::<Result<_>>()?;245246		<Pallet<T>>::create_multiple_items(&self, &caller, amounts, &budget)247			.map_err(dispatch_to_evm::<T>)?;248		Ok(true)249	}250251	#[weight(<SelfWeightOf<T>>::transfer())]252	fn transfer_cross(253		&mut self,254		caller: caller,255		to: EthCrossAccount,256		amount: uint256,257	) -> Result<bool> {258		let caller = T::CrossAccountId::from_eth(caller);259		let to = to.into_sub_cross_account::<T>()?;260		let amount = amount.try_into().map_err(|_| "amount overflow")?;261		let budget = self262			.recorder263			.weight_calls_budget(<StructureWeight<T>>::find_parent());264265		<Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;266		Ok(true)267	}268269	#[weight(<SelfWeightOf<T>>::transfer_from())]270	fn transfer_from_cross(271		&mut self,272		caller: caller,273		from: EthCrossAccount,274		to: EthCrossAccount,275		amount: uint256,276	) -> Result<bool> {277		let caller = T::CrossAccountId::from_eth(caller);278		let from = from.into_sub_cross_account::<T>()?;279		let to = to.into_sub_cross_account::<T>()?;280		let amount = amount.try_into().map_err(|_| "amount overflow")?;281		let budget = self282			.recorder283			.weight_calls_budget(<StructureWeight<T>>::find_parent());284285		<Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)286			.map_err(dispatch_to_evm::<T>)?;287		Ok(true)288	}289}290291#[solidity_interface(292	name = UniqueFungible,293	is(294		ERC20,295		ERC20Mintable,296		ERC20UniqueExtensions,297		Collection(via(common_mut returns CollectionHandle<T>)),298	)299)]300impl<T: Config> FungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}301302generate_stubgen!(gen_impl, UniqueFungibleCall<()>, true);303generate_stubgen!(gen_iface, UniqueFungibleCall<()>, false);304305impl<T: Config> CommonEvmHandler for FungibleHandle<T>306where307	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,308{309	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");310311	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {312		call::<T, UniqueFungibleCall<T>, _, _>(handle, self)313	}314}