--- 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", --- /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 --- 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" --- a/pallets/refungible/src/erc_token.rs +++ b/pallets/refungible/src/erc_token.rs @@ -14,6 +14,11 @@ // You should have received a copy of the GNU General Public License // along with Unique Network. If not, see . +//! # Refungible Pallet EVM API for token pieces +//! +//! Provides ERC-20 standart support implementation and EVM API for unique extensions for Refungible Pallet. +//! Method implementations are mostly doing parameter conversion and calling Nonfungible Pallet methods. + extern crate alloc; use core::{ char::{REPLACEMENT_CHARACTER, decode_utf16}, @@ -40,6 +45,10 @@ #[derive(ToLog)] pub enum ERC20Events { + /// @dev This event is emitted when the amount of tokens (value) is sent + /// from the from address to the to address. In the case of minting new + /// tokens, the transfer is usually from the 0 address while in the case + /// of burning tokens the transfer is to 0. Transfer { #[indexed] from: address, @@ -47,6 +56,8 @@ to: address, value: uint256, }, + /// @dev This event is emitted when the amount of tokens (value) is approved + /// by the owner to be used by the spender. Approval { #[indexed] owner: address, @@ -56,32 +67,49 @@ }, } +/// @title Standard ERC20 token +/// +/// @dev Implementation of the basic standard token. +/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md #[solidity_interface(name = "ERC20", events(ERC20Events))] impl RefungibleTokenHandle { + /// @return the name of the token. fn name(&self) -> Result { Ok(decode_utf16(self.name.iter().copied()) .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER)) .collect::()) } + + /// @return the symbol of the token. fn symbol(&self) -> Result { Ok(string::from_utf8_lossy(&self.token_prefix).into()) } + + /// @dev Total number of tokens in existence fn total_supply(&self) -> Result { self.consume_store_reads(1)?; Ok(>::get((self.id, self.1)).into()) } + /// @dev Not supported fn decimals(&self) -> Result { // Decimals aren't supported for refungible tokens Ok(0) } + /// @dev Gets the balance of the specified address. + /// @param owner The address to query the balance of. + /// @return An uint256 representing the amount owned by the passed address. fn balance_of(&self, owner: address) -> Result { self.consume_store_reads(1)?; let owner = T::CrossAccountId::from_eth(owner); let balance = >::get((self.id, self.1, owner)); Ok(balance.into()) } + + /// @dev Transfer token for a specified address + /// @param to The address to transfer to. + /// @param amount The amount to be transferred. #[weight(>::transfer())] fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result { let caller = T::CrossAccountId::from_eth(caller); @@ -95,6 +123,11 @@ .map_err(|_| "transfer error")?; Ok(true) } + + /// @dev Transfer tokens from one address to another + /// @param from address The address which you want to send tokens from + /// @param to address The address which you want to transfer to + /// @param amount uint256 the amount of tokens to be transferred #[weight(>::transfer_from())] fn transfer_from( &mut self, @@ -115,6 +148,14 @@ .map_err(dispatch_to_evm::)?; Ok(true) } + + /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`. + /// Beware that changing an allowance with this method brings the risk that someone may use both the old + /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this + /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards: + /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 + /// @param spender The address which will spend the funds. + /// @param amount The amount of tokens to be spent. #[weight(>::approve())] fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result { let caller = T::CrossAccountId::from_eth(caller); @@ -125,6 +166,11 @@ .map_err(dispatch_to_evm::)?; Ok(true) } + + /// @dev Function to check the amount of tokens that an owner allowed to a spender. + /// @param owner address The address which owns the funds. + /// @param spender address The address which will spend the funds. + /// @return A uint256 specifying the amount of tokens still available for the spender. fn allowance(&self, owner: address, spender: address) -> Result { self.consume_store_reads(1)?; let owner = T::CrossAccountId::from_eth(owner); @@ -136,6 +182,10 @@ #[solidity_interface(name = "ERC20UniqueExtensions")] impl RefungibleTokenHandle { + /// @dev Function that burns an amount of the token of a given account, + /// deducting from the sender's allowance for said account. + /// @param from The account whose tokens will be burnt. + /// @param amount The amount that will be burnt. #[weight(>::burn_from())] fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result { let caller = T::CrossAccountId::from_eth(caller); @@ -150,6 +200,9 @@ Ok(true) } + /// @dev Function that changes total amount of the tokens. + /// Throws if `msg.sender` doesn't owns all of the tokens. + /// @param amount New total amount of the tokens. #[weight(>::repartition_item())] fn repartition(&mut self, caller: caller, amount: uint256) -> Result { let caller = T::CrossAccountId::from_eth(caller);