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
14// You should have received a copy of the GNU General Public License14// You should have received a copy of the GNU General Public License
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
16
17//! # Refungible Pallet EVM API for token pieces
18//!
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.
1621
17extern crate alloc;22extern crate alloc;
18use core::{23use core::{
4045
41#[derive(ToLog)]46#[derive(ToLog)]
42pub enum ERC20Events {47pub enum ERC20Events {
48 /// @dev This event is emitted when the amount of tokens (value) is sent
49 /// from the from address to the to address. In the case of minting new
50 /// tokens, the transfer is usually from the 0 address while in the case
51 /// of burning tokens the transfer is to 0.
43 Transfer {52 Transfer {
44 #[indexed]53 #[indexed]
45 from: address,54 from: address,
46 #[indexed]55 #[indexed]
47 to: address,56 to: address,
48 value: uint256,57 value: uint256,
49 },58 },
59 /// @dev This event is emitted when the amount of tokens (value) is approved
60 /// by the owner to be used by the spender.
50 Approval {61 Approval {
51 #[indexed]62 #[indexed]
52 owner: address,63 owner: address,
56 },67 },
57}68}
5869
70/// @title Standard ERC20 token
71///
72/// @dev Implementation of the basic standard token.
73/// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
59#[solidity_interface(name = "ERC20", events(ERC20Events))]74#[solidity_interface(name = "ERC20", events(ERC20Events))]
60impl<T: Config> RefungibleTokenHandle<T> {75impl<T: Config> RefungibleTokenHandle<T> {
76 /// @return the name of the token.
61 fn name(&self) -> Result<string> {77 fn name(&self) -> Result<string> {
62 Ok(decode_utf16(self.name.iter().copied())78 Ok(decode_utf16(self.name.iter().copied())
63 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))79 .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
64 .collect::<string>())80 .collect::<string>())
65 }81 }
82
83 /// @return the symbol of the token.
66 fn symbol(&self) -> Result<string> {84 fn symbol(&self) -> Result<string> {
67 Ok(string::from_utf8_lossy(&self.token_prefix).into())85 Ok(string::from_utf8_lossy(&self.token_prefix).into())
68 }86 }
87
88 /// @dev Total number of tokens in existence
69 fn total_supply(&self) -> Result<uint256> {89 fn total_supply(&self) -> Result<uint256> {
70 self.consume_store_reads(1)?;90 self.consume_store_reads(1)?;
71 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())91 Ok(<TotalSupply<T>>::get((self.id, self.1)).into())
72 }92 }
7393
94 /// @dev Not supported
74 fn decimals(&self) -> Result<uint8> {95 fn decimals(&self) -> Result<uint8> {
75 // Decimals aren't supported for refungible tokens96 // Decimals aren't supported for refungible tokens
76 Ok(0)97 Ok(0)
77 }98 }
7899
100 /// @dev Gets the balance of the specified address.
101 /// @param owner The address to query the balance of.
102 /// @return An uint256 representing the amount owned by the passed address.
79 fn balance_of(&self, owner: address) -> Result<uint256> {103 fn balance_of(&self, owner: address) -> Result<uint256> {
80 self.consume_store_reads(1)?;104 self.consume_store_reads(1)?;
81 let owner = T::CrossAccountId::from_eth(owner);105 let owner = T::CrossAccountId::from_eth(owner);
82 let balance = <Balance<T>>::get((self.id, self.1, owner));106 let balance = <Balance<T>>::get((self.id, self.1, owner));
83 Ok(balance.into())107 Ok(balance.into())
84 }108 }
109
110 /// @dev Transfer token for a specified address
111 /// @param to The address to transfer to.
112 /// @param amount The amount to be transferred.
85 #[weight(<CommonWeights<T>>::transfer())]113 #[weight(<CommonWeights<T>>::transfer())]
86 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {114 fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {
87 let caller = T::CrossAccountId::from_eth(caller);115 let caller = T::CrossAccountId::from_eth(caller);
96 Ok(true)124 Ok(true)
97 }125 }
126
127 /// @dev Transfer tokens from one address to another
128 /// @param from address The address which you want to send tokens from
129 /// @param to address The address which you want to transfer to
130 /// @param amount uint256 the amount of tokens to be transferred
98 #[weight(<CommonWeights<T>>::transfer_from())]131 #[weight(<CommonWeights<T>>::transfer_from())]
99 fn transfer_from(132 fn transfer_from(
100 &mut self,133 &mut self,
116 Ok(true)149 Ok(true)
117 }150 }
151
152 /// @dev Approve the passed address to spend the specified amount of tokens on behalf of `msg.sender`.
153 /// Beware that changing an allowance with this method brings the risk that someone may use both the old
154 /// and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
155 /// race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
156 /// https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
157 /// @param spender The address which will spend the funds.
158 /// @param amount The amount of tokens to be spent.
118 #[weight(<SelfWeightOf<T>>::approve())]159 #[weight(<SelfWeightOf<T>>::approve())]
119 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {160 fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {
120 let caller = T::CrossAccountId::from_eth(caller);161 let caller = T::CrossAccountId::from_eth(caller);
126 Ok(true)167 Ok(true)
127 }168 }
169
170 /// @dev Function to check the amount of tokens that an owner allowed to a spender.
171 /// @param owner address The address which owns the funds.
172 /// @param spender address The address which will spend the funds.
173 /// @return A uint256 specifying the amount of tokens still available for the spender.
128 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {174 fn allowance(&self, owner: address, spender: address) -> Result<uint256> {
129 self.consume_store_reads(1)?;175 self.consume_store_reads(1)?;
130 let owner = T::CrossAccountId::from_eth(owner);176 let owner = T::CrossAccountId::from_eth(owner);
136182
137#[solidity_interface(name = "ERC20UniqueExtensions")]183#[solidity_interface(name = "ERC20UniqueExtensions")]
138impl<T: Config> RefungibleTokenHandle<T> {184impl<T: Config> RefungibleTokenHandle<T> {
185 /// @dev Function that burns an amount of the token of a given account,
186 /// deducting from the sender's allowance for said account.
187 /// @param from The account whose tokens will be burnt.
188 /// @param amount The amount that will be burnt.
139 #[weight(<SelfWeightOf<T>>::burn_from())]189 #[weight(<SelfWeightOf<T>>::burn_from())]
140 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {190 fn burn_from(&mut self, caller: caller, from: address, amount: uint256) -> Result<bool> {
141 let caller = T::CrossAccountId::from_eth(caller);191 let caller = T::CrossAccountId::from_eth(caller);
150 Ok(true)200 Ok(true)
151 }201 }
152202
203 /// @dev Function that changes total amount of the tokens.
204 /// Throws if `msg.sender` doesn't owns all of the tokens.
205 /// @param amount New total amount of the tokens.
153 #[weight(<SelfWeightOf<T>>::repartition_item())]206 #[weight(<SelfWeightOf<T>>::repartition_item())]
154 fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {207 fn repartition(&mut self, caller: caller, amount: uint256) -> Result<bool> {
155 let caller = T::CrossAccountId::from_eth(caller);208 let caller = T::CrossAccountId::from_eth(caller);