git.delta.rocks / unique-network / refs/commits / 4fbe604fc85f

difftreelog

source

pallets/refungible/src/erc_token.rs6.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/>.1617extern crate alloc;18use core::{19	char::{REPLACEMENT_CHARACTER, decode_utf16},20	convert::TryInto,21	ops::Deref,22};23use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};24use pallet_common::{25	CommonWeightInfo,26	erc::{CommonEvmHandler, PrecompileResult},27};28use pallet_evm::{account::CrossAccountId, PrecompileHandle};29use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};30use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};31use sp_std::vec::Vec;32use up_data_structs::TokenId;3334use crate::{35	Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,36	weights::WeightInfo, TotalSupply,37};3839pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4041#[derive(ToLog)]42pub enum ERC20Events {43	Transfer {44		#[indexed]45		from: address,46		#[indexed]47		to: address,48		value: uint256,49	},50	Approval {51		#[indexed]52		owner: address,53		#[indexed]54		spender: address,55		value: uint256,56	},57}5859#[solidity_interface(name = "ERC20", events(ERC20Events))]60impl<T: Config> RefungibleTokenHandle<T> {61	fn name(&self) -> Result<string> {62		Ok(decode_utf16(self.name.iter().copied())63			.map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))64			.collect::<string>())65	}66	fn symbol(&self) -> Result<string> {67		Ok(string::from_utf8_lossy(&self.token_prefix).into())68	}69	fn total_supply(&self) -> Result<uint256> {70		self.consume_store_reads(1)?;71		Ok(<TotalSupply<T>>::get((self.id, self.1)).into())72	}7374	fn decimals(&self) -> Result<uint8> {75		// Decimals aren't supported for refungible tokens76		Ok(0)77	}7879	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, self.1, owner));83		Ok(balance.into())84	}85	#[weight(<CommonWeights<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, self.1, amount, &budget)95			.map_err(|_| "transfer error")?;96		Ok(true)97	}98	#[weight(<CommonWeights<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, self.1, 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, self.1, 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, self.1, owner, spender)).into())134	}135}136137#[solidity_interface(name = "ERC20UniqueExtensions")]138impl<T: Config> RefungibleTokenHandle<T> {139	#[weight(<SelfWeightOf<T>>::burn_from())]140	fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {141		let caller = T::CrossAccountId::from_eth(caller);142		let from = T::CrossAccountId::from_eth(from);143		let amount = amount.try_into().map_err(|_| "amount overflow")?;144		let budget = self145			.recorder146			.weight_calls_budget(<StructureWeight<T>>::find_parent());147148		<Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)149			.map_err(dispatch_to_evm::<T>)?;150		Ok(true)151	}152}153154impl<T: Config> RefungibleTokenHandle<T> {155	pub fn into_inner(self) -> RefungibleHandle<T> {156		self.0157	}158	pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {159		&mut self.0160	}161}162163impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {164	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {165		self.0.recorder()166	}167	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {168		self.0.into_recorder()169	}170}171172impl<T: Config> Deref for RefungibleTokenHandle<T> {173	type Target = RefungibleHandle<T>;174175	fn deref(&self) -> &Self::Target {176		&self.0177	}178}179180#[solidity_interface(name = "UniqueRefungibleToken", is(ERC20, ERC20UniqueExtensions,))]181impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}182183generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);184generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);185186impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>187where188	T::AccountId: From<[u8; 32]>,189{190	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");191192	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {193		call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)194	}195}