git.delta.rocks / unique-network / refs/commits / 60243736a5d5

difftreelog

doc(refungible-pallet): add documentation for ERC20 EVM API

Grigoriy Simonov2022-07-22parent: #d58cb79.patch.diff
in: master

4 files changed

modifiedCargo.lockdiffbeforeafterboth
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6320,7 +6320,7 @@
 
 [[package]]
 name = "pallet-refungible"
-version = "0.1.1"
+version = "0.1.2"
 dependencies = [
  "ethereum",
  "evm-coder",
addedpallets/refungible/CHANGELOG.mddiffbeforeafterboth
--- /dev/null
+++ b/pallets/refungible/CHANGELOG.md
@@ -0,0 +1,6 @@
+## v0.1.2 - 2022-07
+
+### Refungible Pallet
+
+feat(refungible-pallet): add ERC-20 EVM API for RFT token pieces ([#413](https://github.com/UniqueNetwork/unique-chain/pull/413))
+test(refungible-pallet): add tests for ERC-20 EVM API for RFT token pieces ([#413](https://github.com/UniqueNetwork/unique-chain/pull/413))
\ No newline at end of file
modifiedpallets/refungible/Cargo.tomldiffbeforeafterboth
--- a/pallets/refungible/Cargo.toml
+++ b/pallets/refungible/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "pallet-refungible"
-version = "0.1.1"
+version = "0.1.2"
 license = "GPLv3"
 edition = "2021"
 
modifiedpallets/refungible/src/erc_token.rsdiffbeforeafterboth
before · pallets/refungible/src/erc_token.rs
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	}152153	#[weight(<SelfWeightOf<T>>::repartition_item())]154	fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {155		let caller = T::CrossAccountId::from_eth(caller);156		let amount = amount.try_into().map_err(|_| "amount overflow")?;157158		<Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;159		Ok(true)160	}161}162163impl<T: Config> RefungibleTokenHandle<T> {164	pub fn into_inner(self) -> RefungibleHandle<T> {165		self.0166	}167	pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {168		&mut self.0169	}170}171172impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {173	fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {174		self.0.recorder()175	}176	fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {177		self.0.into_recorder()178	}179}180181impl<T: Config> Deref for RefungibleTokenHandle<T> {182	type Target = RefungibleHandle<T>;183184	fn deref(&self) -> &Self::Target {185		&self.0186	}187}188189#[solidity_interface(name = "UniqueRefungibleToken", is(ERC20, ERC20UniqueExtensions,))]190impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}191192generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);193generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);194195impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>196where197	T::AccountId: From<[u8; 32]>,198{199	const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");200201	fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {202		call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)203	}204}