git.delta.rocks / unique-network / refs/commits / 0766e08cb8d3

difftreelog

doc(pallet-fungible): document public api

PraetorP2022-07-13parent: #956ec6e.patch.diff
in: master

3 files changed

modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
104 }104 }
105}105}
106106
107/// Implementation of `CommonCollectionOperations` for `FungibleHandle`. It wraps FungibleHandle Pallete
108/// methods and adds weight info.
107impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {109impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
108 fn create_item(110 fn create_item(
109 &self,111 &self,
modifiedpallets/fungible/src/erc.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//! ERC-20 standart support implementation.
1618
17use core::char::{REPLACEMENT_CHARACTER, decode_utf16};19use core::char::{REPLACEMENT_CHARACTER, decode_utf16};
18use core::convert::TryInto;20use core::convert::TryInto;
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):
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.
61 FungibleItemsHaveNoId,123 FungibleItemsHaveNoId,
62 /// Tried to set data for fungible item124 /// Tried to set data for fungible item.
63 FungibleItemsDontHaveData,125 FungibleItemsDontHaveData,
64 /// Fungible token does not support nested126 /// Fungible token does not support nesting.
65 FungibleDisallowsNesting,127 FungibleDisallowsNesting,
66 /// Setting item properties is not allowed128 /// Setting item properties is not allowed.
67 SettingPropertiesNotAllowed,129 SettingPropertiesNotAllowed,
68 }130 }
69131
78 #[pallet::generate_store(pub(super) trait Store)]140 #[pallet::generate_store(pub(super) trait Store)]
79 pub struct Pallet<T>(_);141 pub struct Pallet<T>(_);
80142
143 /// Total amount of fungible tokens inside a collection.
81 #[pallet::storage]144 #[pallet::storage]
82 pub type TotalSupply<T: Config> =145 pub type TotalSupply<T: Config> =
83 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;146 StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;
84147
148 /// Amount of tokens owned by an account inside a collection.
85 #[pallet::storage]149 #[pallet::storage]
86 pub type Balance<T: Config> = StorageNMap<150 pub type Balance<T: Config> = StorageNMap<
87 Key = (151 Key = (
92 QueryKind = ValueQuery,156 QueryKind = ValueQuery,
93 >;157 >;
94158
159 /// Storage for delegated assets.
95 #[pallet::storage]160 #[pallet::storage]
96 pub type Allowance<T: Config> = StorageNMap<161 pub type Allowance<T: Config> = StorageNMap<
97 Key = (162 Key = (
103 QueryKind = ValueQuery,168 QueryKind = ValueQuery,
104 >;169 >;
105}170}
106171/// Handler for fungible assets.
107pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);172pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);
108impl<T: Config> FungibleHandle<T> {173impl<T: Config> FungibleHandle<T> {
174 /// Casts [pallet_common::CollectionHandle] into [FungibleHandle].
109 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {175 pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {
110 Self(inner)176 Self(inner)
111 }177 }
178
179 /// Casts [FungibleHandle] into [pallet_common::CollectionHandle].
112 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {180 pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
113 self.0181 self.0
114 }182 }
183 /// Returns a mutable reference to the internal [pallet_common::CollectionHandle].
115 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {184 pub fn common_mut(&mut self) -> &mut pallet_common::CollectionHandle<T> {
116 &mut self.0185 &mut self.0
117 }186 }
132 }201 }
133}202}
134203
204/// Pallet implementation for fungible assets
135impl<T: Config> Pallet<T> {205impl<T: Config> Pallet<T> {
206 /// Initializes the collection. Returns [CollectionId] on success, [DispatchError] otherwise.
136 pub fn init_collection(207 pub fn init_collection(
137 owner: T::CrossAccountId,208 owner: T::CrossAccountId,
138 data: CreateCollectionData<T::AccountId>,209 data: CreateCollectionData<T::AccountId>,
139 ) -> Result<CollectionId, DispatchError> {210 ) -> Result<CollectionId, DispatchError> {
140 <PalletCommon<T>>::init_collection(owner, data, false)211 <PalletCommon<T>>::init_collection(owner, data, false)
141 }212 }
213
214 /// Destroys a collection.
142 pub fn destroy_collection(215 pub fn destroy_collection(
143 collection: FungibleHandle<T>,216 collection: FungibleHandle<T>,
144 sender: &T::CrossAccountId,217 sender: &T::CrossAccountId,
159 Ok(())232 Ok(())
160 }233 }
161234
235 ///Checks if collection has tokens. Return `true` if it has.
162 fn collection_has_tokens(collection_id: CollectionId) -> bool {236 fn collection_has_tokens(collection_id: CollectionId) -> bool {
163 <TotalSupply<T>>::get(collection_id) != 0237 <TotalSupply<T>>::get(collection_id) != 0
164 }238 }
165239
240 /// Burns the specified amount of the token. If the token balance
241 /// or total supply is less than the given value,
242 /// it will return [DispatchError].
166 pub fn burn(243 pub fn burn(
167 collection: &FungibleHandle<T>,244 collection: &FungibleHandle<T>,
168 owner: &T::CrossAccountId,245 owner: &T::CrossAccountId,
207 Ok(())284 Ok(())
208 }285 }
209286
287 /// Transfers the specified amount of tokens. Will check that
288 /// the transfer is allowed for the token.
210 pub fn transfer(289 pub fn transfer(
211 collection: &FungibleHandle<T>,290 collection: &FungibleHandle<T>,
212 from: &T::CrossAccountId,291 from: &T::CrossAccountId,
277 Ok(())356 Ok(())
278 }357 }
279358
359 /// Minting tokens for multiple IDs.
280 pub fn create_multiple_items(360 pub fn create_multiple_items(
281 collection: &FungibleHandle<T>,361 collection: &FungibleHandle<T>,
282 sender: &T::CrossAccountId,362 sender: &T::CrossAccountId,
378 ));458 ));
379 }459 }
380460
461 /// Sets the amount of owner tokens that the spender can manage.
381 pub fn set_allowance(462 pub fn set_allowance(
382 collection: &FungibleHandle<T>,463 collection: &FungibleHandle<T>,
383 owner: &T::CrossAccountId,464 owner: &T::CrossAccountId,
441 Ok(allowance)522 Ok(allowance)
442 }523 }
443524
525 /// Transfers of tokens that `from` gave to `spender` to manage, to `to` ID.
444 pub fn transfer_from(526 pub fn transfer_from(
445 collection: &FungibleHandle<T>,527 collection: &FungibleHandle<T>,
446 spender: &T::CrossAccountId,528 spender: &T::CrossAccountId,
460 Ok(())542 Ok(())
461 }543 }
462544
545 /// Burns managed tokens.
463 pub fn burn_from(546 pub fn burn_from(
464 collection: &FungibleHandle<T>,547 collection: &FungibleHandle<T>,
465 spender: &T::CrossAccountId,548 spender: &T::CrossAccountId,