git.delta.rocks / unique-network / refs/commits / 586a87efbd8a

difftreelog

source

pallets/refungible/src/erc_token.rs13.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//! # Refungible Pallet EVM API for token pieces18//!19//! Provides ERC-20 standart support implementation and EVM API for unique extensions for Refungible Pallet.20//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods.2122use core::{23	char::{REPLACEMENT_CHARACTER, decode_utf16},24	convert::TryInto,25	ops::Deref,26};27use evm_coder::{abi::AbiType, ToLog, generate_stubgen, solidity_interface, types::*};28use pallet_common::{29	erc::{CommonEvmHandler, PrecompileResult},30	eth::{collection_id_to_address, CrossAddress},31	CommonWeightInfo,32};33use pallet_evm::{account::CrossAccountId, PrecompileHandle};34use pallet_evm_coder_substrate::{35	call, dispatch_to_evm, WithRecorder, frontier_contract,36	execution::{Result, PreDispatch},37};38use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};39use sp_std::vec::Vec;40use sp_core::U256;41use up_data_structs::TokenId;4243use crate::{Allowance, Balance, Config, Pallet, RefungibleHandle, TotalSupply, common::CommonWeights, SelfWeightOf, weights::WeightInfo};4445/// Refungible token handle contains information about token's collection and id46///47/// RefungibleTokenHandle doesn't check token's existance upon creation48pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4950frontier_contract! {51	macro_rules! RefungibleTokenHandle_result {...}52	impl<T: Config> Contract for RefungibleTokenHandle<T> {...}53}5455#[solidity_interface(name = ERC1633, enum(derive(PreDispatch)), enum_attr(weight))]56impl<T: Config> RefungibleTokenHandle<T> {57	fn parent_token(&self) -> Address {58		collection_id_to_address(self.id)59	}6061	fn parent_token_id(&self) -> U256 {62		self.1.into()63	}64}6566#[derive(ToLog)]67pub enum ERC20Events {68	/// @dev This event is emitted when the amount of tokens (value) is sent69	/// from the from address to the to address. In the case of minting new70	/// tokens, the transfer is usually from the 0 address while in the case71	/// of burning tokens the transfer is to 0.72	Transfer {73		#[indexed]74		from: Address,75		#[indexed]76		to: Address,77		value: U256,78	},79	/// @dev This event is emitted when the amount of tokens (value) is approved80	/// by the owner to be used by the spender.81	Approval {82		#[indexed]83		owner: Address,84		#[indexed]85		spender: Address,86		value: U256,87	},88}8990/// @title Standard ERC20 token91///92/// @dev Implementation of the basic standard token.93/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md94#[solidity_interface(name = ERC20, events(ERC20Events), enum(derive(PreDispatch)), enum_attr(weight))]95impl<T: Config> RefungibleTokenHandle<T> {96	/// @return the name of the token.97	fn name(&self) -> String {98		decode_utf16(self.name.iter().copied())99			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))100			.collect::<String>()101	}102103	/// @return the symbol of the token.104	fn symbol(&self) -> String {105		String::from_utf8_lossy(&self.token_prefix).into()106	}107108	/// @dev Total number of tokens in existence109	fn total_supply(&self) -> Result<U256> {110		self.consume_store_reads(1)?;111		Ok(<TotalSupply<T>>::get((self.id, self.1)).into())112	}113114	/// @dev Not supported115	fn decimals(&self) -> Result<u8> {116		// Decimals aren't supported for refungible tokens117		Ok(0)118	}119120	/// @dev Gets the balance of the specified address.121	/// @param owner The address to query the balance of.122	/// @return An uint256 representing the amount owned by the passed address.123	fn balance_of(&self, owner: Address) -> Result<U256> {124		self.consume_store_reads(1)?;125		let owner = T::CrossAccountId::from_eth(owner);126		let balance = <Balance<T>>::get((self.id, self.1, owner));127		Ok(balance.into())128	}129130	/// @dev Transfer token for a specified address131	/// @param to The address to transfer to.132	/// @param amount The amount to be transferred.133	#[weight(<CommonWeights<T>>::transfer())]134	fn transfer(&mut self, caller: Caller, to: Address, amount: U256) -> Result<bool> {135		let caller = T::CrossAccountId::from_eth(caller);136		let to = T::CrossAccountId::from_eth(to);137		let amount = amount.try_into().map_err(|_| "amount overflow")?;138		let budget = self139			.recorder140			.weight_calls_budget(<StructureWeight<T>>::find_parent());141142		<Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)143			.map_err(dispatch_to_evm::<T>)?;144		Ok(true)145	}146147	/// @dev Transfer tokens from one address to another148	/// @param from address The address which you want to send tokens from149	/// @param to address The address which you want to transfer to150	/// @param amount uint256 the amount of tokens to be transferred151	#[weight(<CommonWeights<T>>::transfer_from())]152	fn transfer_from(153		&mut self,154		caller: Caller,155		from: Address,156		to: Address,157		amount: U256,158	) -> Result<bool> {159		let caller = T::CrossAccountId::from_eth(caller);160		let from = T::CrossAccountId::from_eth(from);161		let to = T::CrossAccountId::from_eth(to);162		let amount = amount.try_into().map_err(|_| "amount overflow")?;163		let budget = self164			.recorder165			.weight_calls_budget(<StructureWeight<T>>::find_parent());166167		<Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)168			.map_err(dispatch_to_evm::<T>)?;169		Ok(true)170	}171172	/// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.173	/// Beware that changing an allowance with this method brings the risk that someone may use both the old174	/// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this175	/// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:176	/// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729177	/// @param spender The address which will spend the funds.178	/// @param amount The amount of tokens to be spent.179	#[weight(<SelfWeightOf<T>>::approve())]180	fn approve(&mut self, caller: Caller, spender: Address, amount: U256) -> Result<bool> {181		let caller = T::CrossAccountId::from_eth(caller);182		let spender = T::CrossAccountId::from_eth(spender);183		let amount = amount.try_into().map_err(|_| "amount overflow")?;184185		<Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)186			.map_err(dispatch_to_evm::<T>)?;187		Ok(true)188	}189190	/// @dev Function to check the amount of tokens that an owner allowed to a spender.191	/// @param owner address The address which owns the funds.192	/// @param spender address The address which will spend the funds.193	/// @return A uint256 specifying the amount of tokens still available for the spender.194	fn allowance(&self, owner: Address, spender: Address) -> Result<U256> {195		self.consume_store_reads(1)?;196		let owner = T::CrossAccountId::from_eth(owner);197		let spender = T::CrossAccountId::from_eth(spender);198199		Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())200	}201}202203#[solidity_interface(name = ERC20UniqueExtensions, enum(derive(PreDispatch)), enum_attr(weight))]204impl<T: Config> RefungibleTokenHandle<T>205where206	T::AccountId: From<[u8; 32]>,207{208	/// @dev Function to check the amount of tokens that an owner allowed to a spender.209	/// @param owner crossAddress The address which owns the funds.210	/// @param spender crossAddress The address which will spend the funds.211	/// @return A uint256 specifying the amount of tokens still available for the spender.212	fn allowance_cross(&self, owner: CrossAddress, spender: CrossAddress) -> Result<U256> {213		let owner = owner.into_sub_cross_account::<T>()?;214		let spender = spender.into_sub_cross_account::<T>()?;215216		Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())217	}218219	/// @dev Function that burns an amount of the token of a given account,220	/// deducting from the sender's allowance for said account.221	/// @param from The account whose tokens will be burnt.222	/// @param amount The amount that will be burnt.223	#[weight(<SelfWeightOf<T>>::burn_from())]224	#[solidity(hide)]225	fn burn_from(&mut self, caller: Caller, from: Address, amount: U256) -> Result<bool> {226		let caller = T::CrossAccountId::from_eth(caller);227		let from = T::CrossAccountId::from_eth(from);228		let amount = amount.try_into().map_err(|_| "amount overflow")?;229		let budget = self230			.recorder231			.weight_calls_budget(<StructureWeight<T>>::find_parent());232233		<Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)234			.map_err(dispatch_to_evm::<T>)?;235		Ok(true)236	}237238	/// @dev Function that burns an amount of the token of a given account,239	/// deducting from the sender's allowance for said account.240	/// @param from The account whose tokens will be burnt.241	/// @param amount The amount that will be burnt.242	#[weight(<SelfWeightOf<T>>::burn_from())]243	fn burn_from_cross(244		&mut self,245		caller: Caller,246		from: CrossAddress,247		amount: U256,248	) -> Result<bool> {249		let caller = T::CrossAccountId::from_eth(caller);250		let from = from.into_sub_cross_account::<T>()?;251		let amount = amount.try_into().map_err(|_| "amount overflow")?;252		let budget = self253			.recorder254			.weight_calls_budget(<StructureWeight<T>>::find_parent());255256		<Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)257			.map_err(dispatch_to_evm::<T>)?;258		Ok(true)259	}260261	/// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.262	/// Beware that changing an allowance with this method brings the risk that someone may use both the old263	/// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this264	/// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:265	/// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729266	/// @param spender The crossaccount which will spend the funds.267	/// @param amount The amount of tokens to be spent.268	#[weight(<SelfWeightOf<T>>::approve())]269	fn approve_cross(270		&mut self,271		caller: Caller,272		spender: CrossAddress,273		amount: U256,274	) -> Result<bool> {275		let caller = T::CrossAccountId::from_eth(caller);276		let spender = spender.into_sub_cross_account::<T>()?;277		let amount = amount.try_into().map_err(|_| "amount overflow")?;278279		<Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)280			.map_err(dispatch_to_evm::<T>)?;281		Ok(true)282	}283	/// @dev Function that changes total amount of the tokens.284	///  Throws if `msg.sender` doesn't owns all of the tokens.285	/// @param amount New total amount of the tokens.286	#[weight(<SelfWeightOf<T>>::repartition_item())]287	fn repartition(&mut self, caller: Caller, amount: U256) -> Result<bool> {288		let caller = T::CrossAccountId::from_eth(caller);289		let amount = amount.try_into().map_err(|_| "amount overflow")?;290291		<Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;292		Ok(true)293	}294295	/// @dev Transfer token for a specified address296	/// @param to The crossaccount to transfer to.297	/// @param amount The amount to be transferred.298	#[weight(<CommonWeights<T>>::transfer())]299	fn transfer_cross(&mut self, caller: Caller, to: CrossAddress, amount: U256) -> Result<bool> {300		let caller = T::CrossAccountId::from_eth(caller);301		let to = to.into_sub_cross_account::<T>()?;302		let amount = amount.try_into().map_err(|_| "amount overflow")?;303		let budget = self304			.recorder305			.weight_calls_budget(<StructureWeight<T>>::find_parent());306307		<Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)308			.map_err(dispatch_to_evm::<T>)?;309		Ok(true)310	}311312	/// @dev Transfer tokens from one address to another313	/// @param from The address which you want to send tokens from314	/// @param to The address which you want to transfer to315	/// @param amount the amount of tokens to be transferred316	#[weight(<CommonWeights<T>>::transfer_from())]317	fn transfer_from_cross(318		&mut self,319		caller: Caller,320		from: CrossAddress,321		to: CrossAddress,322		amount: U256,323	) -> Result<bool> {324		let caller = T::CrossAccountId::from_eth(caller);325		let from = from.into_sub_cross_account::<T>()?;326		let to = to.into_sub_cross_account::<T>()?;327		let amount = amount.try_into().map_err(|_| "amount overflow")?;328		let budget = self329			.recorder330			.weight_calls_budget(<StructureWeight<T>>::find_parent());331332		<Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)333			.map_err(dispatch_to_evm::<T>)?;334		Ok(true)335	}336}337338impl<T: Config> RefungibleTokenHandle<T> {339	pub fn into_inner(self) -> RefungibleHandle<T> {340		self.0341	}342	pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {343		&mut self.0344	}345}346347impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {348	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {349		self.0.recorder()350	}351	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {352		self.0.into_recorder()353	}354}355356impl<T: Config> Deref for RefungibleTokenHandle<T> {357	type Target = RefungibleHandle<T>;358359	fn deref(&self) -> &Self::Target {360		&self.0361	}362}363364#[solidity_interface(365	name = UniqueRefungibleToken,366	is(ERC20, ERC20UniqueExtensions, ERC1633),367	enum(derive(PreDispatch)),368)]369impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}370371generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);372generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);373374impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>375where376	T::AccountId: From<[u8; 32]>,377{378	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");379380	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {381		call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)382	}383}