git.delta.rocks / unique-network / refs/commits / 49a12f2c8527

difftreelog

Merge pull request #437 from UniqueNetwork/doc/pallet-fungible

Yaroslav Bolyukin2022-07-19parents: #e2730ed #95d6df7.patch.diff
in: master
doc(pallet-fungible): document public api

3 files changed

modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
--- a/pallets/fungible/src/common.rs
+++ b/pallets/fungible/src/common.rs
@@ -105,6 +105,8 @@
 	}
 }
 
+/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete
+/// methods and adds weight info.
 impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
 	fn create_item(
 		&self,
modifiedpallets/fungible/src/erc.rsdiffbeforeafterboth
--- a/pallets/fungible/src/erc.rs
+++ b/pallets/fungible/src/erc.rs
@@ -14,6 +14,8 @@
 // You should have received a copy of the GNU General Public License
 // along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
 
+//! ERC-20 standart support implementation.
+
 use core::char::{REPLACEMENT_CHARACTER, decode_utf16};
 use core::convert::TryInto;
 use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*, weight};
modifiedpallets/fungible/src/lib.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//! # Fungible Pallet
18//!
19//! The Fungible pallet provides functionality for dealing with fungible assets.
20//!
21//! - [`CreateItemData`]
22//! - [`Config`]
23//! - [`FungibleHandle`]
24//! - [`Pallet`]
25//! - [`TotalSupply`]
26//! - [`Balance`]
27//! - [`Allowance`]
28//! - [`Error`]
29//!
30//! ## Fungible tokens
31//!
32//! Fungible tokens or assets are divisible and non-unique. For instance,
33//! fiat currencies like the dollar are fungible: A $1 bill
34//! in New York City has the same value as a $1 bill in Miami.
35//! A fungible token can also be a cryptocurrency like Bitcoin: 1 BTC is worth 1 BTC,
36//! no matter where it is issued. Thus, the fungibility refers to a specific currency’s
37//! ability to maintain one standard value. As well, it needs to have uniform acceptance.
38//! This means that a currency’s history should not be able to affect its value,
39//! and this is due to the fact that each piece that is a part of the currency is equal
40//! in value when compared to every other piece of that exact same currency.
41//! In the world of cryptocurrencies, this is essentially a coin or a token
42//! that can be replaced by another identical coin or token, and they are
43//! both mutually interchangeable. A popular implementation of fungible tokens is
44//! the ERC-20 token standard.
45//!
46//! ### ERC-20
47//!
48//! The [ERC-20](https://ethereum.org/en/developers/docs/standards/tokens/erc-20/) (Ethereum Request for Comments 20), proposed by Fabian Vogelsteller in November 2015,
49//! is a Token Standard that implements an API for tokens within Smart Contracts.
50//!
51//! Example functionalities ERC-20 provides:
52//!
53//! * transfer tokens from one account to another
54//! * get the current token balance of an account
55//! * get the total supply of the token available on the network
56//! * approve whether an amount of token from an account can be spent by a third-party account
57//!
58//! ## Overview
59//!
60//! The module provides functionality for asset management of fungible asset, supports ERC-20 standart, includes:
61//!
62//! * Asset Issuance
63//! * Asset Transferal
64//! * Asset Destruction
65//! * Delegated Asset Transfers
66//!
67//! **NOTE:** The created fungible asset always has `token_id` = 0.
68//! So `tokenA` and `tokenB` will have different `collection_id`.
69//!
70//! ### Implementations
71//!
72//! The Fungible pallet provides implementations for the following traits.
73//!
74//! - [`WithRecorder`](pallet_evm_coder_substrate::WithRecorder): Trait for EVM support
75//! - [`CommonCollectionOperations`](pallet_common::CommonCollectionOperations): Functions for dealing with collections
76//! - [`CommonWeightInfo`](pallet_common::CommonWeightInfo): Functions for retrieval of transaction weight
77//! - [`CommonEvmHandler`](pallet_common::erc::CommonEvmHandler): Function for handling EVM runtime calls
1678
17#![cfg_attr(not(feature = "std"), no_std)]79#![cfg_attr(not(feature = "std"), no_std)]
1880
57 pub enum Error<T> {119 pub enum Error<T> {
58 /// Not Fungible item data used to mint in Fungible collection.120 /// Not Fungible item data used to mint in Fungible collection.
59 NotFungibleDataUsedToMintFungibleCollectionToken,121 NotFungibleDataUsedToMintFungibleCollectionToken,
60 /// Not default id passed as TokenId argument122 /// Not default id passed as TokenId argument.
123 /// The default value of TokenId for Fungible collection is 0.
61 FungibleItemsHaveNoId,124 FungibleItemsHaveNoId,
62 /// Tried to set data for fungible item125 /// Tried to set data for fungible item.
63 FungibleItemsDontHaveData,126 FungibleItemsDontHaveData,
64 /// Fungible token does not support nested127 /// Fungible token does not support nesting.
65 FungibleDisallowsNesting,128 FungibleDisallowsNesting,
66 /// Setting item properties is not allowed129 /// Setting item properties is not allowed.
67 SettingPropertiesNotAllowed,130 SettingPropertiesNotAllowed,
68 }131 }
69132
78 #[pallet::generate_store(pub(super) trait Store)]141 #[pallet::generate_store(pub(super) trait Store)]
79 pub struct Pallet<T>(_);142 pub struct Pallet<T>(_);
80143
144 /// Total amount of fungible tokens inside a collection.
81 #[pallet::storage]145 #[pallet::storage]
82 pub type TotalSupply<T: Config> =146 pub type TotalSupply<T: Config> =
83 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;147 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;
84148
149 /// Amount of tokens owned by an account inside a collection.
85 #[pallet::storage]150 #[pallet::storage]
86 pub type Balance<T: Config> = StorageNMap<151 pub type Balance<T: Config> = StorageNMap<
87 Key = (152 Key = (
92 QueryKind = ValueQuery,157 QueryKind = ValueQuery,
93 >;158 >;
94159
160 /// Storage for delegated assets.
95 #[pallet::storage]161 #[pallet::storage]
96 pub type Allowance<T: Config> = StorageNMap<162 pub type Allowance<T: Config> = StorageNMap<
97 Key = (163 Key = (
104 >;170 >;
105}171}
172
173/// Wrapper around untyped collection handle, asserting inner collection is of fungible type.
174/// Required for interaction with Fungible collections, type safety and implementation [`solidity_interface`][`evm_coder::solidity_interface`].
106175
107pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);176pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);
177
178/// Implementation of methods required for dispatching during runtime.
108impl<T: Config> FungibleHandle<T> {179impl<T: Config> FungibleHandle<T> {
180 /// Casts [`CollectionHandle`][`pallet_common::CollectionHandle`] into [`FungibleHandle`].
109 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {181 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {
110 Self(inner)182 Self(inner)
111 }183 }
184
185 /// Casts [`FungibleHandle`] into [`CollectionHandle`][`pallet_common::CollectionHandle`].
112 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {186 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
113 self.0187 self.0
114 }188 }
189 /// Returns a mutable reference to the internal [`CollectionHandle`][`pallet_common::CollectionHandle`].
115 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {190 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {
116 &mut self.0191 &mut self.0
117 }192 }
132 }207 }
133}208}
134209
210/// Pallet implementation for fungible assets
135impl<T: Config> Pallet<T> {211impl<T: Config> Pallet<T> {
212 /// Initializes the collection. Returns [CollectionId] on success, [DispatchError] otherwise.
136 pub fn init_collection(213 pub fn init_collection(
137 owner: T::CrossAccountId,214 owner: T::CrossAccountId,
138 data: CreateCollectionData<T::AccountId>,215 data: CreateCollectionData<T::AccountId>,
139 ) -> Result<CollectionId, DispatchError> {216 ) -> Result<CollectionId, DispatchError> {
140 <PalletCommon<T>>::init_collection(owner, data, false)217 <PalletCommon<T>>::init_collection(owner, data, false)
141 }218 }
219
220 /// Destroys a collection.
142 pub fn destroy_collection(221 pub fn destroy_collection(
143 collection: FungibleHandle<T>,222 collection: FungibleHandle<T>,
144 sender: &T::CrossAccountId,223 sender: &T::CrossAccountId,
159 Ok(())238 Ok(())
160 }239 }
161240
241 ///Checks if collection has tokens. Return `true` if it has.
162 fn collection_has_tokens(collection_id: CollectionId) -> bool {242 fn collection_has_tokens(collection_id: CollectionId) -> bool {
163 <TotalSupply<T>>::get(collection_id) != 0243 <TotalSupply<T>>::get(collection_id) != 0
164 }244 }
165245
246 /// Burns the specified amount of the token. If the token balance
247 /// or total supply is less than the given value,
248 /// it will return [DispatchError].
166 pub fn burn(249 pub fn burn(
167 collection: &FungibleHandle<T>,250 collection: &FungibleHandle<T>,
168 owner: &T::CrossAccountId,251 owner: &T::CrossAccountId,
207 Ok(())290 Ok(())
208 }291 }
209292
293 /// Transfers the specified amount of tokens. Will check that
294 /// the transfer is allowed for the token.
295 ///
296 /// - `from`: Owner of tokens to transfer.
297 /// - `to`: Recepient of transfered tokens.
298 /// - `amount`: Amount of tokens to transfer.
299 /// - `collection`: Collection that contains the token
210 pub fn transfer(300 pub fn transfer(
211 collection: &FungibleHandle<T>,301 collection: &FungibleHandle<T>,
212 from: &T::CrossAccountId,302 from: &T::CrossAccountId,
277 Ok(())367 Ok(())
278 }368 }
279369
370 /// Minting tokens for multiple IDs.
371 /// See [`create_item`][`Pallet::create_item`] for more details.
280 pub fn create_multiple_items(372 pub fn create_multiple_items(
281 collection: &FungibleHandle<T>,373 collection: &FungibleHandle<T>,
282 sender: &T::CrossAccountId,374 sender: &T::CrossAccountId,
378 ));470 ));
379 }471 }
380472
473 /// Set allowance for the spender to `transfer` or `burn` owner's tokens.
474 ///
475 /// - `collection`: Collection that contains the token
476 /// - `owner`: Owner of tokens that sets the allowance.
477 /// - `spender`: Recipient of the allowance rights.
478 /// - `amount`: Amount of tokens the spender is allowed to `transfer` or `burn`.
381 pub fn set_allowance(479 pub fn set_allowance(
382 collection: &FungibleHandle<T>,480 collection: &FungibleHandle<T>,
383 owner: &T::CrossAccountId,481 owner: &T::CrossAccountId,
402 Ok(())500 Ok(())
403 }501 }
404502
503 /// Checks if a non-owner has (enough) allowance from the owner to perform operations on the tokens.
504 /// Returns the expected remaining allowance - it should be set manually if the transaction proceeds.
505 ///
506 /// - `collection`: Collection that contains the token.
507 /// - `spender`: CrossAccountId who has the allowance rights.
508 /// - `from`: The owner of the tokens who sets the allowance.
509 /// - `amount`: Amount of tokens by which the allowance sholud be reduced.
405 fn check_allowed(510 fn check_allowed(
406 collection: &FungibleHandle<T>,511 collection: &FungibleHandle<T>,
407 spender: &T::CrossAccountId,512 spender: &T::CrossAccountId,
441 Ok(allowance)546 Ok(allowance)
442 }547 }
548
549 /// Transfer fungible tokens from one account to another.
550 /// Same as the [`transfer`][`Pallet::transfer`] but spender doesn't needs to be an owner of the token pieces.
551 /// The owner should set allowance for the spender to transfer pieces.
552 /// See [`set_allowance`][`Pallet::set_allowance`] for more details.
443553
444 pub fn transfer_from(554 pub fn transfer_from(
445 collection: &FungibleHandle<T>,555 collection: &FungibleHandle<T>,
460 Ok(())570 Ok(())
461 }571 }
462572
573 /// Burn fungible tokens from the account.
574 ///
575 /// Same as the [`burn`][`Pallet::burn`] but spender doesn't need to be an owner of the tokens. The `from` should
576 /// set allowance for the spender to burn tokens.
577 /// See [`set_allowance`][`Pallet::set_allowance`] for more details.
463 pub fn burn_from(578 pub fn burn_from(
464 collection: &FungibleHandle<T>,579 collection: &FungibleHandle<T>,
465 spender: &T::CrossAccountId,580 spender: &T::CrossAccountId,
478 Ok(())593 Ok(())
479 }594 }
480595
481 /// Delegated to `create_multiple_items`596 /// Creates fungible token.
597 ///
598 /// The sender should be the owner/admin of the collection or collection should be configured
599 /// to allow public minting.
600 ///
601 /// - `data`: Contains user who will become the owners of the tokens and amount
602 /// of tokens he will receive.
482 pub fn create_item(603 pub fn create_item(
483 collection: &FungibleHandle<T>,604 collection: &FungibleHandle<T>,
484 sender: &T::CrossAccountId,605 sender: &T::CrossAccountId,