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

difftreelog

source

pallets/fungible/src/erc.rs10.1 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::{28	CollectionHandle,29	erc::{CommonEvmHandler, PrecompileResult, CollectionCall},30	eth::EthCrossAccount,31};32use sp_std::vec::Vec;33use pallet_evm::{account::CrossAccountId, PrecompileHandle};34use pallet_evm_coder_substrate::{call, dispatch_to_evm};35use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};36use sp_core::Get;3738use crate::{39	Allowance, Balance, Config, FungibleHandle, Pallet, SelfWeightOf, TotalSupply,40	weights::WeightInfo,41};4243#[derive(ToLog)]44pub enum ERC20Events {45	Transfer {46		#[indexed]47		from: address,48		#[indexed]49		to: address,50		value: uint256,51	},52	Approval {53		#[indexed]54		owner: address,55		#[indexed]56		spender: address,57		value: uint256,58	},59}6061#[solidity_interface(name = ERC20, events(ERC20Events))]62impl<T: Config> FungibleHandle<T> {63	fn name(&self) -> Result<string> {64		Ok(decode_utf16(self.name.iter().copied())65			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))66			.collect::<string>())67	}68	fn symbol(&self) -> Result<string> {69		Ok(string::from_utf8_lossy(&self.token_prefix).into())70	}71	fn total_supply(&self) -> Result<uint256> {72		self.consume_store_reads(1)?;73		Ok(<TotalSupply<T>>::get(self.id).into())74	}7576	fn decimals(&self) -> Result<uint8> {77		Ok(if let CollectionMode::Fungible(decimals) = &self.mode {78			*decimals79		} else {80			unreachable!()81		})82	}83	fn balance_of(&self, owner: address) -> Result<uint256> {84		self.consume_store_reads(1)?;85		let owner = T::CrossAccountId::from_eth(owner);86		let balance = <Balance<T>>::get((self.id, owner));87		Ok(balance.into())88	}89	#[weight(<SelfWeightOf<T>>::transfer())]90	fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {91		let caller = T::CrossAccountId::from_eth(caller);92		let to = T::CrossAccountId::from_eth(to);93		let amount = amount.try_into().map_err(|_| "amount overflow")?;94		let budget = self95			.recorder96			.weight_calls_budget(<StructureWeight<T>>::find_parent());9798		<Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;99		Ok(true)100	}101102	#[weight(<SelfWeightOf<T>>::transfer_from())]103	fn transfer_from(104		&mut self,105		caller: caller,106		from: address,107		to: address,108		amount: uint256,109	) -> Result<bool> {110		let caller = T::CrossAccountId::from_eth(caller);111		let from = T::CrossAccountId::from_eth(from);112		let to = T::CrossAccountId::from_eth(to);113		let amount = amount.try_into().map_err(|_| "amount overflow")?;114		let budget = self115			.recorder116			.weight_calls_budget(<StructureWeight<T>>::find_parent());117118		<Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)119			.map_err(dispatch_to_evm::<T>)?;120		Ok(true)121	}122	#[weight(<SelfWeightOf<T>>::approve())]123	fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {124		let caller = T::CrossAccountId::from_eth(caller);125		let spender = T::CrossAccountId::from_eth(spender);126		let amount = amount.try_into().map_err(|_| "amount overflow")?;127128		<Pallet<T>>::set_allowance(self, &caller, &spender, amount)129			.map_err(dispatch_to_evm::<T>)?;130		Ok(true)131	}132	fn allowance(&self, owner: address, spender: address) -> Result<uint256> {133		self.consume_store_reads(1)?;134		let owner = T::CrossAccountId::from_eth(owner);135		let spender = T::CrossAccountId::from_eth(spender);136137		Ok(<Allowance<T>>::get((self.id, owner, spender)).into())138	}139140	/// @notice Returns collection helper contract address141	fn collection_helper_address(&self) -> Result<address> {142		Ok(T::ContractAddress::get())143	}144}145146#[solidity_interface(name = ERC20Mintable)]147impl<T: Config> FungibleHandle<T> {148	/// Mint tokens for `to` account.149	/// @param to account that will receive minted tokens150	/// @param amount amount of tokens to mint151	#[weight(<SelfWeightOf<T>>::create_item())]152	fn mint(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {153		let caller = T::CrossAccountId::from_eth(caller);154		let to = T::CrossAccountId::from_eth(to);155		let amount = amount.try_into().map_err(|_| "amount overflow")?;156		let budget = self157			.recorder158			.weight_calls_budget(<StructureWeight<T>>::find_parent());159		<Pallet<T>>::create_item(&self, &caller, (to, amount), &budget)160			.map_err(dispatch_to_evm::<T>)?;161		Ok(true)162	}163}164165#[solidity_interface(name = ERC20UniqueExtensions)]166impl<T: Config> FungibleHandle<T>167where168	T::AccountId: From<[u8; 32]>,169{170	/// @notice A description for the collection.171	fn description(&self) -> Result<string> {172		Ok(decode_utf16(self.description.iter().copied())173			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))174			.collect::<string>())175	}176177	#[weight(<SelfWeightOf<T>>::approve())]178	fn approve_cross(179		&mut self,180		caller: caller,181		spender: EthCrossAccount,182		amount: uint256,183	) -> Result<bool> {184		let caller = T::CrossAccountId::from_eth(caller);185		let spender = spender.into_sub_cross_account::<T>()?;186		let amount = amount.try_into().map_err(|_| "amount overflow")?;187188		<Pallet<T>>::set_allowance(self, &caller, &spender, amount)189			.map_err(dispatch_to_evm::<T>)?;190		Ok(true)191	}192193	/// Burn tokens from account194	/// @dev Function that burns an `amount` of the tokens of a given account,195	/// deducting from the sender's allowance for said account.196	/// @param from The account whose tokens will be burnt.197	/// @param amount The amount that will be burnt.198	#[solidity(hide)]199	#[weight(<SelfWeightOf<T>>::burn_from())]200	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {201		let caller = T::CrossAccountId::from_eth(caller);202		let from = T::CrossAccountId::from_eth(from);203		let amount = amount.try_into().map_err(|_| "amount overflow")?;204		let budget = self205			.recorder206			.weight_calls_budget(<StructureWeight<T>>::find_parent());207208		<Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)209			.map_err(dispatch_to_evm::<T>)?;210		Ok(true)211	}212213	/// Burn tokens from account214	/// @dev Function that burns an `amount` of the tokens of a given account,215	/// deducting from the sender's allowance for said account.216	/// @param from The account whose tokens will be burnt.217	/// @param amount The amount that will be burnt.218	#[weight(<SelfWeightOf<T>>::burn_from())]219	fn burn_from_cross(220		&mut self,221		caller: caller,222		from: EthCrossAccount,223		amount: uint256,224	) -> Result<bool> {225		let caller = T::CrossAccountId::from_eth(caller);226		let from = from.into_sub_cross_account::<T>()?;227		let amount = amount.try_into().map_err(|_| "amount overflow")?;228		let budget = self229			.recorder230			.weight_calls_budget(<StructureWeight<T>>::find_parent());231232		<Pallet<T>>::burn_from(self, &caller, &from, amount, &budget)233			.map_err(dispatch_to_evm::<T>)?;234		Ok(true)235	}236237	/// Mint tokens for multiple accounts.238	/// @param amounts array of pairs of account address and amount239	#[weight(<SelfWeightOf<T>>::create_multiple_items_ex(amounts.len() as u32))]240	fn mint_bulk(&mut self, caller: caller, amounts: Vec<(address, uint256)>) -> Result<bool> {241		let caller = T::CrossAccountId::from_eth(caller);242		let budget = self243			.recorder244			.weight_calls_budget(<StructureWeight<T>>::find_parent());245		let amounts = amounts246			.into_iter()247			.map(|(to, amount)| {248				Ok((249					T::CrossAccountId::from_eth(to),250					amount.try_into().map_err(|_| "amount overflow")?,251				))252			})253			.collect::<Result<_>>()?;254255		<Pallet<T>>::create_multiple_items(&self, &caller, amounts, &budget)256			.map_err(dispatch_to_evm::<T>)?;257		Ok(true)258	}259260	#[weight(<SelfWeightOf<T>>::transfer())]261	fn transfer_cross(262		&mut self,263		caller: caller,264		to: EthCrossAccount,265		amount: uint256,266	) -> Result<bool> {267		let caller = T::CrossAccountId::from_eth(caller);268		let to = to.into_sub_cross_account::<T>()?;269		let amount = amount.try_into().map_err(|_| "amount overflow")?;270		let budget = self271			.recorder272			.weight_calls_budget(<StructureWeight<T>>::find_parent());273274		<Pallet<T>>::transfer(self, &caller, &to, amount, &budget).map_err(|_| "transfer error")?;275		Ok(true)276	}277278	#[weight(<SelfWeightOf<T>>::transfer_from())]279	fn transfer_from_cross(280		&mut self,281		caller: caller,282		from: EthCrossAccount,283		to: EthCrossAccount,284		amount: uint256,285	) -> Result<bool> {286		let caller = T::CrossAccountId::from_eth(caller);287		let from = from.into_sub_cross_account::<T>()?;288		let to = to.into_sub_cross_account::<T>()?;289		let amount = amount.try_into().map_err(|_| "amount overflow")?;290		let budget = self291			.recorder292			.weight_calls_budget(<StructureWeight<T>>::find_parent());293294		<Pallet<T>>::transfer_from(self, &caller, &from, &to, amount, &budget)295			.map_err(dispatch_to_evm::<T>)?;296		Ok(true)297	}298}299300#[solidity_interface(301	name = UniqueFungible,302	is(303		ERC20,304		ERC20Mintable,305		ERC20UniqueExtensions,306		Collection(via(common_mut returns CollectionHandle<T>)),307	)308)]309impl<T: Config> FungibleHandle<T> where T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]> {}310311generate_stubgen!(gen_impl, UniqueFungibleCall<()>, true);312generate_stubgen!(gen_iface, UniqueFungibleCall<()>, false);313314impl<T: Config> CommonEvmHandler for FungibleHandle<T>315where316	T::AccountId: From<[u8; 32]> + AsRef<[u8; 32]>,317{318	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");319320	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {321		call::<T, UniqueFungibleCall<T>, _, _>(handle, self)322	}323}