difftreelog
refactor split refungible into its own pallet
in: master
8 files changed
pallets/refungible/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/Cargo.toml
@@ -0,0 +1,31 @@
+[package]
+name = "pallet-refungible"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies.codec]
+default-features = false
+features = ['derive']
+package = 'parity-scale-codec'
+version = '2.0.0'
+
+[dependencies]
+frame-support = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+frame-system = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+sp-runtime = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+sp-std = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+sp-core = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+pallet-common = { default-features = false, path = '../common' }
+nft-data-structs = { default-features = false, path = '../../primitives/nft' }
+
+[features]
+default = ["std"]
+std = [
+ "frame-support/std",
+ "frame-system/std",
+ "sp-runtime/std",
+ "sp-std/std",
+ "nft-data-structs/std",
+ "pallet-common/std",
+]
+runtime-benchmarks = []
pallets/refungible/src/benchmarking.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/src/benchmarking.rs
@@ -0,0 +1 @@
+#![cfg(feature = "runtime-benchmarking")]
pallets/refungible/src/common.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/src/common.rs
@@ -0,0 +1,201 @@
+use core::marker::PhantomData;
+
+use sp_std::collections::btree_map::BTreeMap;
+use frame_support::{dispatch::DispatchResultWithPostInfo, fail, weights::Weight};
+use nft_data_structs::TokenId;
+use pallet_common::{
+ CommonCollectionOperations, CommonWeightInfo, account::CrossAccountId, with_weight,
+};
+use sp_runtime::DispatchError;
+use sp_std::vec::Vec;
+
+use crate::{
+ AccountBalance, Allowance, Balance, Config, CreateItemData, DataKind, Error, Owned, Pallet,
+ RefungibleHandle, SelfWeightOf, TokenData, weights::WeightInfo,
+};
+
+pub struct CommonWeights<T: Config>(PhantomData<T>);
+impl<T: Config> CommonWeightInfo for CommonWeights<T> {
+ fn create_item() -> Weight {
+ <SelfWeightOf<T>>::create_item()
+ }
+
+ fn create_multiple_items(amount: u32) -> Weight {
+ <SelfWeightOf<T>>::create_multiple_items(amount)
+ }
+
+ fn burn_item() -> Weight {
+ <SelfWeightOf<T>>::burn_item()
+ }
+
+ fn transfer() -> Weight {
+ <SelfWeightOf<T>>::transfer()
+ }
+
+ fn approve() -> Weight {
+ <SelfWeightOf<T>>::approve()
+ }
+
+ fn transfer_from() -> Weight {
+ <SelfWeightOf<T>>::transfer_from()
+ }
+
+ fn set_variable_metadata(_bytes: u32) -> Weight {
+ <SelfWeightOf<T>>::set_variable_metadata()
+ }
+}
+
+fn map_create_data<T: Config>(
+ data: nft_data_structs::CreateItemData,
+ to: &T::CrossAccountId,
+) -> Result<CreateItemData<T>, DispatchError> {
+ match data {
+ nft_data_structs::CreateItemData::ReFungible(data) => Ok(CreateItemData {
+ const_data: data.const_data,
+ variable_data: data.variable_data,
+ users: {
+ let mut out = BTreeMap::new();
+ out.insert(to.clone(), data.pieces);
+ out
+ },
+ }),
+ _ => fail!(<Error<T>>::NotRefungibleDataUsedToMintFungibleCollectionToken),
+ }
+}
+
+impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {
+ fn create_item(
+ &self,
+ sender: T::CrossAccountId,
+ to: T::CrossAccountId,
+ data: nft_data_structs::CreateItemData,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::create_item(self, &sender, map_create_data(data, &to)?),
+ <SelfWeightOf<T>>::create_item(),
+ )
+ }
+
+ fn create_multiple_items(
+ &self,
+ sender: T::CrossAccountId,
+ to: T::CrossAccountId,
+ data: Vec<nft_data_structs::CreateItemData>,
+ ) -> DispatchResultWithPostInfo {
+ let data = data
+ .into_iter()
+ .map(|d| map_create_data::<T>(d, &to))
+ .collect::<Result<Vec<_>, DispatchError>>()?;
+
+ let amount = data.len();
+ with_weight(
+ <Pallet<T>>::create_multiple_items(self, &sender, data),
+ <SelfWeightOf<T>>::create_multiple_items(amount as u32),
+ )
+ }
+
+ fn burn_item(
+ &self,
+ sender: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::burn(self, &sender, token, amount),
+ <SelfWeightOf<T>>::burn_item(),
+ )
+ }
+
+ fn transfer(
+ &self,
+ from: T::CrossAccountId,
+ to: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::transfer(&self, &from, &to, token, amount),
+ <SelfWeightOf<T>>::transfer(),
+ )
+ }
+
+ fn approve(
+ &self,
+ sender: T::CrossAccountId,
+ spender: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::set_allowance(&self, &sender, &spender, token, amount),
+ <SelfWeightOf<T>>::approve(),
+ )
+ }
+
+ fn transfer_from(
+ &self,
+ sender: T::CrossAccountId,
+ from: T::CrossAccountId,
+ to: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::transfer_from(&self, &sender, &from, &to, token, amount),
+ <SelfWeightOf<T>>::approve(),
+ )
+ }
+
+ fn set_variable_metadata(
+ &self,
+ sender: T::CrossAccountId,
+ token: TokenId,
+ data: Vec<u8>,
+ ) -> DispatchResultWithPostInfo {
+ with_weight(
+ <Pallet<T>>::set_variable_metadata(&self, &sender, token, data),
+ <SelfWeightOf<T>>::set_variable_metadata(),
+ )
+ }
+
+ fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {
+ <Owned<T>>::iter_prefix((self.id, account.as_sub()))
+ .map(|(id, _)| id)
+ .collect()
+ }
+
+ fn token_exists(&self, token: TokenId) -> bool {
+ <Pallet<T>>::token_exists(self, token)
+ }
+
+ fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {
+ T::CrossAccountId::default()
+ }
+ fn const_metadata(&self, token: TokenId) -> Vec<u8> {
+ <TokenData<T>>::get((self.id, token, DataKind::Constant))
+ }
+ fn variable_metadata(&self, token: TokenId) -> Vec<u8> {
+ <TokenData<T>>::get((self.id, token, DataKind::Variable))
+ }
+
+ fn collection_tokens(&self) -> u32 {
+ <Pallet<T>>::total_supply(self)
+ }
+
+ fn account_balance(&self, account: T::CrossAccountId) -> u32 {
+ <AccountBalance<T>>::get((self.id, account.as_sub()))
+ }
+
+ fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {
+ <Balance<T>>::get((self.id, token, account.as_sub()))
+ }
+
+ fn allowance(
+ &self,
+ sender: T::CrossAccountId,
+ spender: T::CrossAccountId,
+ token: TokenId,
+ ) -> u128 {
+ <Allowance<T>>::get((self.id, token, sender.as_sub(), spender))
+ }
+}
pallets/refungible/src/erc.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/src/erc.rs
@@ -0,0 +1,34 @@
+use nft_data_structs::TokenId;
+use pallet_common::erc::CommonEvmHandler;
+
+use crate::{Config, RefungibleHandle};
+
+impl<T: Config> CommonEvmHandler for RefungibleHandle<T> {
+ const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungible.raw");
+
+ fn call(
+ self,
+ _source: &sp_core::H160,
+ _input: &[u8],
+ _value: sp_core::U256,
+ ) -> Option<pallet_common::erc::PrecompileOutput> {
+ // TODO: Implement RFT variant of ERC721
+ None
+ }
+}
+
+pub struct RefungibleTokenHandle<T: Config>(pub RefungibleHandle<T>, pub TokenId);
+
+impl<T: Config> CommonEvmHandler for RefungibleTokenHandle<T> {
+ const CODE: &'static [u8] = include_bytes!("./stubs/UniqueRefungibleToken.raw");
+
+ fn call(
+ self,
+ _source: &sp_core::H160,
+ _input: &[u8],
+ _value: sp_core::U256,
+ ) -> Option<pallet_common::erc::PrecompileOutput> {
+ // TODO: Implement RFT variant of ERC20
+ None
+ }
+}
pallets/refungible/src/lib.rsdiffbeforeafterbothno changes
pallets/refungible/src/stubs/UniqueRefungible.rawdiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/src/stubs/UniqueRefungible.raw
@@ -0,0 +1 @@
+TODO
pallets/refungible/src/stubs/UniqueRefungibleToken.rawdiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/src/stubs/UniqueRefungibleToken.raw
@@ -0,0 +1 @@
+TODO
pallets/refungible/src/weights.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/refungible/src/weights.rs
@@ -0,0 +1,37 @@
+#![cfg_attr(rustfmt, rustfmt_skip)]
+#![allow(unused_parens)]
+#![allow(unused_imports)]
+
+use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
+use sp_std::marker::PhantomData;
+
+pub trait WeightInfo {
+ fn create_item() -> Weight;
+ fn create_multiple_items(b: u32) -> Weight;
+ fn burn_item() -> Weight;
+ fn transfer() -> Weight;
+ fn approve() -> Weight;
+ fn transfer_from() -> Weight;
+ fn set_variable_metadata() -> Weight;
+}
+
+pub struct SubstrateWeight<T>(PhantomData<T>);
+impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
+ fn create_item() -> Weight {0}
+ fn create_multiple_items(_b: u32) -> Weight {0}
+ fn burn_item() -> Weight {0}
+ fn transfer() -> Weight {0}
+ fn approve() -> Weight {0}
+ fn transfer_from() -> Weight {0}
+ fn set_variable_metadata() -> Weight {0}
+}
+
+impl WeightInfo for () {
+ fn create_item() -> Weight {0}
+ fn create_multiple_items(_b: u32) -> Weight {0}
+ fn burn_item() -> Weight {0}
+ fn transfer() -> Weight {0}
+ fn approve() -> Weight {0}
+ fn transfer_from() -> Weight {0}
+ fn set_variable_metadata() -> Weight {0}
+}