difftreelog
fix Return erc721 instead erc20
in: master
2 files changed
pallets/refungible/src/erc_token.rsdiffbeforeafterboth1// 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.2122extern crate alloc;2324#[cfg(not(feature = "std"))]25use alloc::format;2627use core::{28 char::{REPLACEMENT_CHARACTER, decode_utf16},29 convert::TryInto,30 ops::Deref,31};32use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};33use pallet_common::{34 CommonWeightInfo,35 erc::{CommonEvmHandler, PrecompileResult},36};37use pallet_evm::{account::CrossAccountId, PrecompileHandle};38use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};39use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};40use sp_std::vec::Vec;41use up_data_structs::{mapping::TokenAddressMapping, TokenId};4243use crate::{44 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,45 TotalSupply, weights::WeightInfo,46};4748pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4950#[solidity_interface(name = ERC1633)]51impl<T: Config> RefungibleTokenHandle<T> {52 fn parent_token(&self) -> Result<address> {53 self.consume_store_reads(2)?;54 Ok(*T::CrossTokenAddressMapping::token_to_address(self.id, self.1).as_eth())55 }5657 fn parent_token_id(&self) -> Result<uint256> {58 self.consume_store_reads(2)?;59 Ok(self.1.into())60 }61}6263#[derive(ToLog)]64pub enum ERC20Events {65 /// @dev This event is emitted when the amount of tokens (value) is sent66 /// from the from address to the to address. In the case of minting new67 /// tokens, the transfer is usually from the 0 address while in the case68 /// of burning tokens the transfer is to 0.69 Transfer {70 #[indexed]71 from: address,72 #[indexed]73 to: address,74 value: uint256,75 },76 /// @dev This event is emitted when the amount of tokens (value) is approved77 /// by the owner to be used by the spender.78 Approval {79 #[indexed]80 owner: address,81 #[indexed]82 spender: address,83 value: uint256,84 },85}8687/// @title Standard ERC20 token88///89/// @dev Implementation of the basic standard token.90/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md91#[solidity_interface(name = ERC20, events(ERC20Events))]92impl<T: Config> RefungibleTokenHandle<T> {93 /// @return the name of the token.94 fn name(&self) -> Result<string> {95 Ok(decode_utf16(self.name.iter().copied())96 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))97 .collect::<string>())98 }99100 /// @return the symbol of the token.101 fn symbol(&self) -> Result<string> {102 Ok(string::from_utf8_lossy(&self.token_prefix).into())103 }104105 /// @dev Total number of tokens in existence106 fn total_supply(&self) -> Result<uint256> {107 self.consume_store_reads(1)?;108 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())109 }110111 /// @dev Not supported112 fn decimals(&self) -> Result<uint8> {113 // Decimals aren't supported for refungible tokens114 Ok(0)115 }116117 /// @dev Gets the balance of the specified address.118 /// @param owner The address to query the balance of.119 /// @return An uint256 representing the amount owned by the passed address.120 fn balance_of(&self, owner: address) -> Result<uint256> {121 self.consume_store_reads(1)?;122 let owner = T::CrossAccountId::from_eth(owner);123 let balance = <Balance<T>>::get((self.id, self.1, owner));124 Ok(balance.into())125 }126127 /// @dev Transfer token for a specified address128 /// @param to The address to transfer to.129 /// @param amount The amount to be transferred.130 #[weight(<CommonWeights<T>>::transfer())]131 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {132 let caller = T::CrossAccountId::from_eth(caller);133 let to = T::CrossAccountId::from_eth(to);134 let amount = amount.try_into().map_err(|_| "amount overflow")?;135 let budget = self136 .recorder137 .weight_calls_budget(<StructureWeight<T>>::find_parent());138139 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)140 .map_err(dispatch_to_evm::<T>)?;141 Ok(true)142 }143144 /// @dev Transfer tokens from one address to another145 /// @param from address The address which you want to send tokens from146 /// @param to address The address which you want to transfer to147 /// @param amount uint256 the amount of tokens to be transferred148 #[weight(<CommonWeights<T>>::transfer_from())]149 fn transfer_from(150 &mut self,151 caller: caller,152 from: address,153 to: address,154 amount: uint256,155 ) -> Result<bool> {156 let caller = T::CrossAccountId::from_eth(caller);157 let from = T::CrossAccountId::from_eth(from);158 let to = T::CrossAccountId::from_eth(to);159 let amount = amount.try_into().map_err(|_| "amount overflow")?;160 let budget = self161 .recorder162 .weight_calls_budget(<StructureWeight<T>>::find_parent());163164 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)165 .map_err(dispatch_to_evm::<T>)?;166 Ok(true)167 }168169 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.170 /// Beware that changing an allowance with this method brings the risk that someone may use both the old171 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this172 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:173 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729174 /// @param spender The address which will spend the funds.175 /// @param amount The amount of tokens to be spent.176 #[weight(<SelfWeightOf<T>>::approve())]177 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {178 let caller = T::CrossAccountId::from_eth(caller);179 let spender = T::CrossAccountId::from_eth(spender);180 let amount = amount.try_into().map_err(|_| "amount overflow")?;181182 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)183 .map_err(dispatch_to_evm::<T>)?;184 Ok(true)185 }186187 /// @dev Function to check the amount of tokens that an owner allowed to a spender.188 /// @param owner address The address which owns the funds.189 /// @param spender address The address which will spend the funds.190 /// @return A uint256 specifying the amount of tokens still available for the spender.191 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {192 self.consume_store_reads(1)?;193 let owner = T::CrossAccountId::from_eth(owner);194 let spender = T::CrossAccountId::from_eth(spender);195196 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())197 }198}199200#[solidity_interface(name = ERC20UniqueExtensions)]201impl<T: Config> RefungibleTokenHandle<T> {202 /// @dev Function that burns an amount of the token of a given account,203 /// deducting from the sender's allowance for said account.204 /// @param from The account whose tokens will be burnt.205 /// @param amount The amount that will be burnt.206 #[weight(<SelfWeightOf<T>>::burn_from())]207 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {208 let caller = T::CrossAccountId::from_eth(caller);209 let from = T::CrossAccountId::from_eth(from);210 let amount = amount.try_into().map_err(|_| "amount overflow")?;211 let budget = self212 .recorder213 .weight_calls_budget(<StructureWeight<T>>::find_parent());214215 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)216 .map_err(dispatch_to_evm::<T>)?;217 Ok(true)218 }219220 /// @dev Function that changes total amount of the tokens.221 /// Throws if `msg.sender` doesn't owns all of the tokens.222 /// @param amount New total amount of the tokens.223 #[weight(<SelfWeightOf<T>>::repartition_item())]224 fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {225 let caller = T::CrossAccountId::from_eth(caller);226 let amount = amount.try_into().map_err(|_| "amount overflow")?;227228 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;229 Ok(true)230 }231}232233impl<T: Config> RefungibleTokenHandle<T> {234 pub fn into_inner(self) -> RefungibleHandle<T> {235 self.0236 }237 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {238 &mut self.0239 }240}241242impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {243 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {244 self.0.recorder()245 }246 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {247 self.0.into_recorder()248 }249}250251impl<T: Config> Deref for RefungibleTokenHandle<T> {252 type Target = RefungibleHandle<T>;253254 fn deref(&self) -> &Self::Target {255 &self.0256 }257}258259#[solidity_interface(260 name = UniqueRefungibleToken,261 is(ERC20, ERC20UniqueExtensions, ERC1633)262)]263impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}264265generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);266generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);267268impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>269where270 T::AccountId: From<[u8; 32]>,271{272 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");273274 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {275 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)276 }277}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.2122extern crate alloc;2324#[cfg(not(feature = "std"))]25use alloc::format;2627use core::{28 char::{REPLACEMENT_CHARACTER, decode_utf16},29 convert::TryInto,30 ops::Deref,31};32use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};33use pallet_common::{34 CommonWeightInfo,35 erc::{CommonEvmHandler, PrecompileResult}, eth::collection_id_to_address,36};37use pallet_evm::{account::CrossAccountId, PrecompileHandle};38use pallet_evm_coder_substrate::{call, dispatch_to_evm, WithRecorder};39use pallet_structure::{SelfWeightOf as StructureWeight, weights::WeightInfo as _};40use sp_std::vec::Vec;41use up_data_structs::TokenId;4243use crate::{44 Allowance, Balance, common::CommonWeights, Config, Pallet, RefungibleHandle, SelfWeightOf,45 TotalSupply, weights::WeightInfo,46};4748pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);4950#[solidity_interface(name = ERC1633)]51impl<T: Config> RefungibleTokenHandle<T> {52 fn parent_token(&self) -> Result<address> {53 self.consume_store_reads(2)?;54 Ok(collection_id_to_address(self.id))55 }5657 fn parent_token_id(&self) -> Result<uint256> {58 self.consume_store_reads(2)?;59 Ok(self.1.into())60 }61}6263#[derive(ToLog)]64pub enum ERC20Events {65 /// @dev This event is emitted when the amount of tokens (value) is sent66 /// from the from address to the to address. In the case of minting new67 /// tokens, the transfer is usually from the 0 address while in the case68 /// of burning tokens the transfer is to 0.69 Transfer {70 #[indexed]71 from: address,72 #[indexed]73 to: address,74 value: uint256,75 },76 /// @dev This event is emitted when the amount of tokens (value) is approved77 /// by the owner to be used by the spender.78 Approval {79 #[indexed]80 owner: address,81 #[indexed]82 spender: address,83 value: uint256,84 },85}8687/// @title Standard ERC20 token88///89/// @dev Implementation of the basic standard token.90/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md91#[solidity_interface(name = ERC20, events(ERC20Events))]92impl<T: Config> RefungibleTokenHandle<T> {93 /// @return the name of the token.94 fn name(&self) -> Result<string> {95 Ok(decode_utf16(self.name.iter().copied())96 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))97 .collect::<string>())98 }99100 /// @return the symbol of the token.101 fn symbol(&self) -> Result<string> {102 Ok(string::from_utf8_lossy(&self.token_prefix).into())103 }104105 /// @dev Total number of tokens in existence106 fn total_supply(&self) -> Result<uint256> {107 self.consume_store_reads(1)?;108 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())109 }110111 /// @dev Not supported112 fn decimals(&self) -> Result<uint8> {113 // Decimals aren't supported for refungible tokens114 Ok(0)115 }116117 /// @dev Gets the balance of the specified address.118 /// @param owner The address to query the balance of.119 /// @return An uint256 representing the amount owned by the passed address.120 fn balance_of(&self, owner: address) -> Result<uint256> {121 self.consume_store_reads(1)?;122 let owner = T::CrossAccountId::from_eth(owner);123 let balance = <Balance<T>>::get((self.id, self.1, owner));124 Ok(balance.into())125 }126127 /// @dev Transfer token for a specified address128 /// @param to The address to transfer to.129 /// @param amount The amount to be transferred.130 #[weight(<CommonWeights<T>>::transfer())]131 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {132 let caller = T::CrossAccountId::from_eth(caller);133 let to = T::CrossAccountId::from_eth(to);134 let amount = amount.try_into().map_err(|_| "amount overflow")?;135 let budget = self136 .recorder137 .weight_calls_budget(<StructureWeight<T>>::find_parent());138139 <Pallet<T>>::transfer(self, &caller, &to, self.1, amount, &budget)140 .map_err(dispatch_to_evm::<T>)?;141 Ok(true)142 }143144 /// @dev Transfer tokens from one address to another145 /// @param from address The address which you want to send tokens from146 /// @param to address The address which you want to transfer to147 /// @param amount uint256 the amount of tokens to be transferred148 #[weight(<CommonWeights<T>>::transfer_from())]149 fn transfer_from(150 &mut self,151 caller: caller,152 from: address,153 to: address,154 amount: uint256,155 ) -> Result<bool> {156 let caller = T::CrossAccountId::from_eth(caller);157 let from = T::CrossAccountId::from_eth(from);158 let to = T::CrossAccountId::from_eth(to);159 let amount = amount.try_into().map_err(|_| "amount overflow")?;160 let budget = self161 .recorder162 .weight_calls_budget(<StructureWeight<T>>::find_parent());163164 <Pallet<T>>::transfer_from(self, &caller, &from, &to, self.1, amount, &budget)165 .map_err(dispatch_to_evm::<T>)?;166 Ok(true)167 }168169 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.170 /// Beware that changing an allowance with this method brings the risk that someone may use both the old171 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this172 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:173 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729174 /// @param spender The address which will spend the funds.175 /// @param amount The amount of tokens to be spent.176 #[weight(<SelfWeightOf<T>>::approve())]177 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {178 let caller = T::CrossAccountId::from_eth(caller);179 let spender = T::CrossAccountId::from_eth(spender);180 let amount = amount.try_into().map_err(|_| "amount overflow")?;181182 <Pallet<T>>::set_allowance(self, &caller, &spender, self.1, amount)183 .map_err(dispatch_to_evm::<T>)?;184 Ok(true)185 }186187 /// @dev Function to check the amount of tokens that an owner allowed to a spender.188 /// @param owner address The address which owns the funds.189 /// @param spender address The address which will spend the funds.190 /// @return A uint256 specifying the amount of tokens still available for the spender.191 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {192 self.consume_store_reads(1)?;193 let owner = T::CrossAccountId::from_eth(owner);194 let spender = T::CrossAccountId::from_eth(spender);195196 Ok(<Allowance<T>>::get((self.id, self.1, owner, spender)).into())197 }198}199200#[solidity_interface(name = ERC20UniqueExtensions)]201impl<T: Config> RefungibleTokenHandle<T> {202 /// @dev Function that burns an amount of the token of a given account,203 /// deducting from the sender's allowance for said account.204 /// @param from The account whose tokens will be burnt.205 /// @param amount The amount that will be burnt.206 #[weight(<SelfWeightOf<T>>::burn_from())]207 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {208 let caller = T::CrossAccountId::from_eth(caller);209 let from = T::CrossAccountId::from_eth(from);210 let amount = amount.try_into().map_err(|_| "amount overflow")?;211 let budget = self212 .recorder213 .weight_calls_budget(<StructureWeight<T>>::find_parent());214215 <Pallet<T>>::burn_from(self, &caller, &from, self.1, amount, &budget)216 .map_err(dispatch_to_evm::<T>)?;217 Ok(true)218 }219220 /// @dev Function that changes total amount of the tokens.221 /// Throws if `msg.sender` doesn't owns all of the tokens.222 /// @param amount New total amount of the tokens.223 #[weight(<SelfWeightOf<T>>::repartition_item())]224 fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {225 let caller = T::CrossAccountId::from_eth(caller);226 let amount = amount.try_into().map_err(|_| "amount overflow")?;227228 <Pallet<T>>::repartition(self, &caller, self.1, amount).map_err(dispatch_to_evm::<T>)?;229 Ok(true)230 }231}232233impl<T: Config> RefungibleTokenHandle<T> {234 pub fn into_inner(self) -> RefungibleHandle<T> {235 self.0236 }237 pub fn common_mut(&mut self) -> &mut RefungibleHandle<T> {238 &mut self.0239 }240}241242impl<T: Config> WithRecorder<T> for RefungibleTokenHandle<T> {243 fn recorder(&self) -> &pallet_evm_coder_substrate::SubstrateRecorder<T> {244 self.0.recorder()245 }246 fn into_recorder(self) -> pallet_evm_coder_substrate::SubstrateRecorder<T> {247 self.0.into_recorder()248 }249}250251impl<T: Config> Deref for RefungibleTokenHandle<T> {252 type Target = RefungibleHandle<T>;253254 fn deref(&self) -> &Self::Target {255 &self.0256 }257}258259#[solidity_interface(260 name = UniqueRefungibleToken,261 is(ERC20, ERC20UniqueExtensions, ERC1633)262)]263impl<T: Config> RefungibleTokenHandle<T> where T::AccountId: From<[u8; 32]> {}264265generate_stubgen!(gen_impl, UniqueRefungibleTokenCall<()>, true);266generate_stubgen!(gen_iface, UniqueRefungibleTokenCall<()>, false);267268impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T>269where270 T::AccountId: From<[u8; 32]>,271{272 const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");273274 fn call(self, handle: &mut impl PrecompileHandle) -> Option<PrecompileResult> {275 call::<T, UniqueRefungibleTokenCall<T>, _, _>(handle, self)276 }277}tests/src/eth/reFungibleToken.test.tsdiffbeforeafterboth--- a/tests/src/eth/reFungibleToken.test.ts
+++ b/tests/src/eth/reFungibleToken.test.ts
@@ -668,7 +668,7 @@
const tokenAddress = await refungibleTokenContract.methods.parentToken().call();
const tokenId = await refungibleTokenContract.methods.parentTokenId().call();
- expect(tokenAddress).to.be.equal(rftTokenAddress);
+ expect(tokenAddress).to.be.equal(collectionIdAddress);
expect(tokenId).to.be.equal(refungibleTokenId);
});
});