difftreelog
refactor split fungible into its own pallet
in: master
8 files changed
pallets/fungible/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/pallets/fungible/Cargo.toml
@@ -0,0 +1,36 @@
+[package]
+name = "pallet-fungible"
+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' }
+evm-coder = { default-features = false, path = '../../crates/evm-coder' }
+ethereum = { default-features = false, version = "0.9.0" }
+pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' }
+
+[features]
+default = ["std"]
+std = [
+ "frame-support/std",
+ "frame-system/std",
+ "sp-runtime/std",
+ "sp-std/std",
+ "nft-data-structs/std",
+ "pallet-common/std",
+ "evm-coder/std",
+ "ethereum/std",
+]
+runtime-benchmarks = []
pallets/fungible/src/benchmarking.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/fungible/src/benchmarking.rs
@@ -0,0 +1 @@
+#![cfg(feature = "runtime-benchmarking")]
pallets/fungible/src/common.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/fungible/src/common.rs
@@ -0,0 +1,220 @@
+use core::marker::PhantomData;
+
+use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};
+use nft_data_structs::TokenId;
+use pallet_common::{
+ CommonCollectionOperations, CommonWeightInfo, account::CrossAccountId, with_weight,
+};
+use sp_runtime::ArithmeticError;
+use sp_std::{vec::Vec, vec};
+
+use crate::{
+ Allowance, Balance, Config, Error, FungibleHandle, Pallet, SelfWeightOf, 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 {
+ Self::create_item()
+ }
+
+ 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 {
+ // Error
+ 0
+ }
+}
+
+impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
+ fn create_item(
+ &self,
+ sender: T::CrossAccountId,
+ to: T::CrossAccountId,
+ data: nft_data_structs::CreateItemData,
+ ) -> DispatchResultWithPostInfo {
+ match data {
+ nft_data_structs::CreateItemData::Fungible(data) => with_weight(
+ <Pallet<T>>::create_item(self, &sender, (to, data.value)),
+ <SelfWeightOf<T>>::create_item(),
+ ),
+ _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),
+ }
+ }
+
+ fn create_multiple_items(
+ &self,
+ sender: T::CrossAccountId,
+ to: T::CrossAccountId,
+ data: Vec<nft_data_structs::CreateItemData>,
+ ) -> DispatchResultWithPostInfo {
+ let mut sum: u128 = 0;
+ for data in data {
+ match data {
+ nft_data_structs::CreateItemData::Fungible(data) => {
+ sum = sum
+ .checked_add(data.value)
+ .ok_or(ArithmeticError::Overflow)?;
+ }
+ _ => fail!(<Error<T>>::NotFungibleDataUsedToMintFungibleCollectionToken),
+ }
+ }
+
+ with_weight(
+ <Pallet<T>>::create_item(self, &sender, (to, sum)),
+ <SelfWeightOf<T>>::create_item(),
+ )
+ }
+
+ fn burn_item(
+ &self,
+ sender: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(
+ token == TokenId::default(),
+ <Error<T>>::FungibleItemsHaveNoId
+ );
+
+ with_weight(
+ <Pallet<T>>::burn(self, &sender, amount),
+ <SelfWeightOf<T>>::burn_item(),
+ )
+ }
+
+ fn transfer(
+ &self,
+ from: T::CrossAccountId,
+ to: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(
+ token == TokenId::default(),
+ <Error<T>>::FungibleItemsHaveNoId
+ );
+
+ with_weight(
+ <Pallet<T>>::transfer(&self, &from, &to, amount),
+ <SelfWeightOf<T>>::transfer(),
+ )
+ }
+
+ fn approve(
+ &self,
+ sender: T::CrossAccountId,
+ spender: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(
+ token == TokenId::default(),
+ <Error<T>>::FungibleItemsHaveNoId
+ );
+
+ with_weight(
+ <Pallet<T>>::set_allowance(&self, &sender, &spender, amount),
+ <SelfWeightOf<T>>::approve(),
+ )
+ }
+
+ fn transfer_from(
+ &self,
+ sender: T::CrossAccountId,
+ from: T::CrossAccountId,
+ to: T::CrossAccountId,
+ token: TokenId,
+ amount: u128,
+ ) -> DispatchResultWithPostInfo {
+ ensure!(
+ token == TokenId::default(),
+ <Error<T>>::FungibleItemsHaveNoId
+ );
+
+ with_weight(
+ <Pallet<T>>::transfer_from(&self, &sender, &from, &to, amount),
+ <SelfWeightOf<T>>::transfer_from(),
+ )
+ }
+
+ fn set_variable_metadata(
+ &self,
+ _sender: T::CrossAccountId,
+ _token: TokenId,
+ _data: Vec<u8>,
+ ) -> DispatchResultWithPostInfo {
+ fail!(<Error<T>>::FungibleItemsHaveData)
+ }
+
+ fn account_tokens(&self, account: T::CrossAccountId) -> Vec<TokenId> {
+ if <Balance<T>>::get((self.id, account.as_sub())) != 0 {
+ vec![TokenId::default()]
+ } else {
+ vec![]
+ }
+ }
+
+ fn token_exists(&self, token: TokenId) -> bool {
+ token == TokenId::default()
+ }
+
+ fn token_owner(&self, _token: TokenId) -> T::CrossAccountId {
+ T::CrossAccountId::default()
+ }
+ fn const_metadata(&self, _token: TokenId) -> Vec<u8> {
+ Vec::new()
+ }
+ fn variable_metadata(&self, _token: TokenId) -> Vec<u8> {
+ Vec::new()
+ }
+
+ fn collection_tokens(&self) -> u32 {
+ 1
+ }
+
+ fn account_balance(&self, account: T::CrossAccountId) -> u32 {
+ if <Balance<T>>::get((self.id, account.as_sub())) != 0 {
+ 1
+ } else {
+ 0
+ }
+ }
+
+ fn balance(&self, account: T::CrossAccountId, token: TokenId) -> u128 {
+ if token != TokenId::default() {
+ return 0;
+ }
+ <Balance<T>>::get((self.id, account.as_sub()))
+ }
+
+ fn allowance(
+ &self,
+ sender: T::CrossAccountId,
+ spender: T::CrossAccountId,
+ token: TokenId,
+ ) -> u128 {
+ if token != TokenId::default() {
+ return 0;
+ }
+ <Allowance<T>>::get((self.id, sender.as_sub(), spender.as_sub()))
+ }
+}
pallets/fungible/src/erc.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/fungible/src/erc.rs
@@ -0,0 +1,112 @@
+use core::char::{REPLACEMENT_CHARACTER, decode_utf16};
+use core::convert::TryInto;
+use evm_coder::{ToLog, execution::*, generate_stubgen, solidity_interface, types::*};
+use nft_data_structs::CollectionMode;
+use pallet_common::erc::CommonEvmHandler;
+use sp_core::{H160, U256};
+use sp_std::vec::Vec;
+use pallet_common::account::CrossAccountId;
+use pallet_common::erc::PrecompileOutput;
+use pallet_evm_coder_substrate::{call_internal, dispatch_to_evm};
+
+use crate::{Allowance, Balance, Config, FungibleHandle, Pallet, TotalSupply};
+
+#[derive(ToLog)]
+pub enum ERC20Events {
+ Transfer {
+ #[indexed]
+ from: address,
+ #[indexed]
+ to: address,
+ value: uint256,
+ },
+ Approval {
+ #[indexed]
+ owner: address,
+ #[indexed]
+ spender: address,
+ value: uint256,
+ },
+}
+
+#[solidity_interface(name = "ERC20", events(ERC20Events))]
+impl<T: Config> FungibleHandle<T> {
+ fn name(&self) -> Result<string> {
+ Ok(decode_utf16(self.name.iter().copied())
+ .map(|r| r.unwrap_or(REPLACEMENT_CHARACTER))
+ .collect::<string>())
+ }
+ fn symbol(&self) -> Result<string> {
+ Ok(string::from_utf8_lossy(&self.token_prefix).into())
+ }
+ fn total_supply(&self) -> Result<uint256> {
+ Ok(<TotalSupply<T>>::get(self.id).into())
+ }
+
+ fn decimals(&self) -> Result<uint8> {
+ Ok(if let CollectionMode::Fungible(decimals) = &self.mode {
+ *decimals
+ } else {
+ unreachable!()
+ })
+ }
+ fn balance_of(&self, owner: address) -> Result<uint256> {
+ let owner = T::CrossAccountId::from_eth(owner);
+ let balance = <Balance<T>>::get((self.id, owner.as_sub()));
+ Ok(balance.into())
+ }
+ fn transfer(&mut self, caller: caller, to: address, amount: uint256) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let to = T::CrossAccountId::from_eth(to);
+ let amount = amount.try_into().map_err(|_| "amount overflow")?;
+
+ <Pallet<T>>::transfer(self, &caller, &to, amount).map_err(|_| "transfer error")?;
+ Ok(true)
+ }
+ fn transfer_from(
+ &mut self,
+ caller: caller,
+ from: address,
+ to: address,
+ amount: uint256,
+ ) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let from = T::CrossAccountId::from_eth(from);
+ let to = T::CrossAccountId::from_eth(to);
+ let amount = amount.try_into().map_err(|_| "amount overflow")?;
+
+ <Pallet<T>>::transfer_from(self, &caller, &from, &to, amount)
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
+ }
+ fn approve(&mut self, caller: caller, spender: address, amount: uint256) -> Result<bool> {
+ let caller = T::CrossAccountId::from_eth(caller);
+ let spender = T::CrossAccountId::from_eth(spender);
+ let amount = amount.try_into().map_err(|_| "amount overflow")?;
+
+ <Pallet<T>>::set_allowance(self, &caller, &spender, amount)
+ .map_err(dispatch_to_evm::<T>)?;
+ Ok(true)
+ }
+ fn allowance(&self, owner: address, spender: address) -> Result<uint256> {
+ let owner = T::CrossAccountId::from_eth(owner);
+ let spender = T::CrossAccountId::from_eth(spender);
+
+ Ok(<Allowance<T>>::get((self.id, owner.as_sub(), spender.as_sub())).into())
+ }
+}
+
+#[solidity_interface(name = "UniqueFungible", is(ERC20))]
+impl<T: Config> FungibleHandle<T> {}
+
+generate_stubgen!(get_impl, UniqueFungibleCall, true);
+generate_stubgen!(gen_iface, UniqueFungibleCall, false);
+
+impl<T: Config> CommonEvmHandler for FungibleHandle<T> {
+ const CODE: &'static [u8] = include_bytes!("./stubs/UniqueFungible.raw");
+
+ fn call(mut self, source: &H160, input: &[u8], value: U256) -> Option<PrecompileOutput> {
+ let result = call_internal::<UniqueFungibleCall, _>(*source, &mut self, value, input);
+ self.0.recorder.evm_to_precompile_output(result)
+ }
+}
pallets/fungible/src/lib.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/fungible/src/lib.rs
@@ -0,0 +1,366 @@
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use core::ops::Deref;
+use frame_support::{ensure};
+use nft_data_structs::{AccessMode, Collection, CollectionId, TokenId};
+use pallet_common::{
+ Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, account::CrossAccountId,
+};
+use sp_core::H160;
+use sp_runtime::{ArithmeticError, DispatchError, DispatchResult};
+use sp_std::{vec::Vec, vec, collections::btree_map::BTreeMap};
+
+pub use pallet::*;
+
+use crate::erc::ERC20Events;
+pub mod benchmarking;
+pub mod common;
+pub mod erc;
+pub mod weights;
+
+pub type CreateItemData<T> = (<T as pallet_common::Config>::CrossAccountId, u128);
+pub(crate) type SelfWeightOf<T> = <T as Config>::WeightInfo;
+
+#[frame_support::pallet]
+pub mod pallet {
+ use frame_support::{Blake2_128, Blake2_128Concat, Twox64Concat, pallet_prelude::*, storage::Key};
+ use nft_data_structs::CollectionId;
+ use super::weights::WeightInfo;
+
+ #[pallet::error]
+ pub enum Error<T> {
+ /// Not Fungible item data used to mint in Fungible collection.
+ NotFungibleDataUsedToMintFungibleCollectionToken,
+ /// Not default id passed as TokenId argument
+ FungibleItemsHaveNoId,
+ /// Tried to set data for fungible item
+ FungibleItemsHaveData,
+ }
+
+ #[pallet::config]
+ pub trait Config: frame_system::Config + pallet_common::Config {
+ type WeightInfo: WeightInfo;
+ }
+
+ #[pallet::pallet]
+ #[pallet::generate_store(pub(super) trait Store)]
+ pub struct Pallet<T>(_);
+
+ #[pallet::storage]
+ pub(super) type TotalSupply<T: Config> =
+ StorageMap<Hasher = Twox64Concat, Key = CollectionId, Value = u128, QueryKind = ValueQuery>;
+
+ #[pallet::storage]
+ pub(super) type Balance<T: Config> = StorageNMap<
+ Key = (
+ Key<Twox64Concat, CollectionId>,
+ Key<Blake2_128Concat, T::AccountId>,
+ ),
+ Value = u128,
+ QueryKind = ValueQuery,
+ >;
+
+ #[pallet::storage]
+ pub(super) type Allowance<T: Config> = StorageNMap<
+ Key = (
+ Key<Twox64Concat, CollectionId>,
+ Key<Blake2_128, T::AccountId>,
+ Key<Blake2_128Concat, T::AccountId>,
+ ),
+ Value = u128,
+ QueryKind = ValueQuery,
+ >;
+}
+
+pub struct FungibleHandle<T: Config>(pallet_common::CollectionHandle<T>);
+impl<T: Config> FungibleHandle<T> {
+ pub fn cast(inner: pallet_common::CollectionHandle<T>) -> Self {
+ Self(inner)
+ }
+ pub fn into_inner(self) -> pallet_common::CollectionHandle<T> {
+ self.0
+ }
+}
+impl<T: Config> Deref for FungibleHandle<T> {
+ type Target = pallet_common::CollectionHandle<T>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl<T: Config> Pallet<T> {
+ pub fn init_collection(data: Collection<T>) -> Result<CollectionId, DispatchError> {
+ PalletCommon::init_collection(data)
+ }
+ pub fn destroy_collection(
+ collection: FungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ ) -> DispatchResult {
+ let id = collection.id;
+
+ // =========
+
+ PalletCommon::destroy_collection(collection.0, sender)?;
+
+ <TotalSupply<T>>::remove(id);
+ <Balance<T>>::remove_prefix((id,), None);
+ <Allowance<T>>::remove_prefix((id,), None);
+ Ok(())
+ }
+
+ pub fn burn(
+ collection: &FungibleHandle<T>,
+ owner: &T::CrossAccountId,
+ amount: u128,
+ ) -> DispatchResult {
+ let total_supply = <TotalSupply<T>>::get(collection.id)
+ .checked_sub(amount)
+ .ok_or(<CommonError<T>>::TokenValueTooLow)?;
+
+ let balance = <Balance<T>>::get((collection.id, owner.as_sub()))
+ .checked_sub(amount)
+ .ok_or(<CommonError<T>>::TokenValueTooLow)?;
+
+ if collection.access == AccessMode::WhiteList {
+ collection.check_whitelist(owner)?;
+ }
+
+ // =========
+
+ if balance == 0 {
+ <Balance<T>>::remove((collection.id, owner.as_sub()));
+ } else {
+ <Balance<T>>::insert((collection.id, owner.as_sub()), balance);
+ }
+ <TotalSupply<T>>::insert(collection.id, total_supply);
+
+ collection.log_infallible(ERC20Events::Transfer {
+ from: *owner.as_eth(),
+ to: H160::default(),
+ value: amount.into(),
+ });
+ <PalletCommon<T>>::deposit_event(CommonEvent::ItemDestroyed(
+ collection.id,
+ TokenId::default(),
+ owner.clone(),
+ amount,
+ ));
+ Ok(())
+ }
+
+ pub fn transfer(
+ collection: &FungibleHandle<T>,
+ from: &T::CrossAccountId,
+ to: &T::CrossAccountId,
+ amount: u128,
+ ) -> DispatchResult {
+ ensure!(
+ collection.transfers_enabled,
+ <CommonError<T>>::TransferNotAllowed
+ );
+
+ if collection.access == AccessMode::WhiteList {
+ collection.check_whitelist(from)?;
+ collection.check_whitelist(to)?;
+ }
+ <PalletCommon<T>>::ensure_correct_receiver(to)?;
+
+ let balance_from = <Balance<T>>::get((collection.id, from.as_sub()))
+ .checked_sub(amount)
+ .ok_or(<CommonError<T>>::TokenValueTooLow)?;
+ let balance_to = if from != to {
+ Some(
+ <Balance<T>>::get((collection.id, to.as_sub()))
+ .checked_add(amount)
+ .ok_or(ArithmeticError::Overflow)?,
+ )
+ } else {
+ None
+ };
+
+ collection.consume_sstore()?;
+ collection.consume_sstore()?;
+ collection.consume_log(2, 32)?;
+ collection.consume_sstore()?;
+
+ // =========
+
+ if let Some(balance_to) = balance_to {
+ // from != to
+ if balance_from == 0 {
+ <Balance<T>>::remove((collection.id, from.as_sub()));
+ } else {
+ <Balance<T>>::insert((collection.id, from.as_sub()), balance_from);
+ }
+ <Balance<T>>::insert((collection.id, to.as_sub()), balance_to);
+ }
+
+ collection.log_infallible(ERC20Events::Transfer {
+ from: *from.as_eth(),
+ to: *to.as_eth(),
+ value: amount.into(),
+ });
+ <PalletCommon<T>>::deposit_event(CommonEvent::Transfer(
+ collection.id,
+ TokenId::default(),
+ from.clone(),
+ to.clone(),
+ amount,
+ ));
+ Ok(())
+ }
+
+ pub fn create_multiple_items(
+ collection: &FungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ data: Vec<CreateItemData<T>>,
+ ) -> DispatchResult {
+ let unrestricted_minting = collection.is_owner_or_admin(sender)?;
+ if !unrestricted_minting {
+ ensure!(
+ collection.mint_mode,
+ <CommonError<T>>::PublicMintingNotAllowed
+ );
+ collection.check_whitelist(sender)?;
+
+ for (owner, _) in data.iter() {
+ collection.check_whitelist(owner)?;
+ }
+ }
+
+ let mut balances = BTreeMap::new();
+
+ let total_supply = data
+ .iter()
+ .map(|u| u.1)
+ .try_fold(0u128, |acc, v| acc.checked_add(v))
+ .ok_or(ArithmeticError::Overflow)?;
+
+ for (user, amount) in data.into_iter() {
+ collection.consume_sload()?;
+ let balance = balances
+ .entry(user.clone())
+ .or_insert_with(|| <Balance<T>>::get((collection.id, user.as_sub())));
+ *balance = (*balance)
+ .checked_add(amount)
+ .ok_or(ArithmeticError::Overflow)?;
+ }
+
+ collection.consume_sstore()?;
+ for _ in &balances {
+ collection.consume_sstore()?;
+ collection.consume_log(2, 32)?;
+ collection.consume_sstore()?;
+ }
+
+ // =========
+
+ <TotalSupply<T>>::insert(collection.id, total_supply);
+ for (user, amount) in balances {
+ <Balance<T>>::insert((collection.id, user.as_sub()), amount);
+
+ collection.log_infallible(ERC20Events::Transfer {
+ from: H160::default(),
+ to: *user.as_eth(),
+ value: amount.into(),
+ });
+ <PalletCommon<T>>::deposit_event(CommonEvent::ItemCreated(
+ collection.id,
+ TokenId::default(),
+ user.clone(),
+ amount,
+ ));
+ }
+
+ Ok(())
+ }
+
+ fn set_allowance_unchecked(
+ collection: &FungibleHandle<T>,
+ owner: &T::CrossAccountId,
+ spender: &T::CrossAccountId,
+ amount: u128,
+ ) {
+ <Allowance<T>>::insert((collection.id, owner.as_sub(), spender.as_sub()), amount);
+
+ collection.log_infallible(ERC20Events::Approval {
+ owner: *owner.as_eth(),
+ spender: *spender.as_eth(),
+ value: amount.into(),
+ });
+ <PalletCommon<T>>::deposit_event(CommonEvent::Approved(
+ collection.id,
+ TokenId(0),
+ owner.clone(),
+ spender.clone(),
+ amount,
+ ));
+ }
+
+ pub fn set_allowance(
+ collection: &FungibleHandle<T>,
+ owner: &T::CrossAccountId,
+ spender: &T::CrossAccountId,
+ amount: u128,
+ ) -> DispatchResult {
+ if collection.access == AccessMode::WhiteList {
+ collection.check_whitelist(&owner)?;
+ collection.check_whitelist(&spender)?;
+ }
+
+ if <Balance<T>>::get((collection.id, owner.as_sub())) < amount {
+ ensure!(
+ collection.ignores_owned_amount(owner)?,
+ <CommonError<T>>::CantApproveMoreThanOwned
+ );
+ }
+
+ // =========
+
+ Self::set_allowance_unchecked(collection, owner, spender, amount);
+ Ok(())
+ }
+
+ pub fn transfer_from(
+ collection: &FungibleHandle<T>,
+ spender: &T::CrossAccountId,
+ from: &T::CrossAccountId,
+ to: &T::CrossAccountId,
+ amount: u128,
+ ) -> DispatchResult {
+ if spender == from {
+ return Self::transfer(collection, from, to, amount);
+ }
+ if collection.access == AccessMode::WhiteList {
+ // `from`, `to` checked in [`transfer`]
+ collection.check_whitelist(spender)?;
+ }
+
+ let allowance = <Allowance<T>>::get((collection.id, from.as_sub(), spender.as_sub()))
+ .checked_sub(amount);
+ if allowance.is_none() {
+ ensure!(
+ collection.ignores_allowance(spender)?,
+ <CommonError<T>>::TokenValueNotEnough
+ );
+ }
+
+ // =========
+
+ Self::transfer(collection, from, to, amount)?;
+ if let Some(allowance) = allowance {
+ Self::set_allowance_unchecked(collection, from, spender, allowance);
+ }
+ Ok(())
+ }
+
+ /// Delegated to `create_multiple_items`
+ pub fn create_item(
+ collection: &FungibleHandle<T>,
+ sender: &T::CrossAccountId,
+ data: CreateItemData<T>,
+ ) -> DispatchResult {
+ Self::create_multiple_items(collection, sender, vec![data])
+ }
+}
pallets/fungible/src/stubs/UniqueFungible.rawdiffbeforeafterbothbinary blob — no preview
pallets/fungible/src/stubs/UniqueFungible.soldiffbeforeafterboth1// SPDX-License-Identifier: OTHER2// This code is automatically generated34pragma solidity >=0.8.0 <0.9.0;56// Common stubs holder7contract Dummy {8 uint8 dummy;9 string stub_error = "this contract is implemented in native";10}1112// Inline13contract ERC20Events {14 event Transfer(address indexed from, address indexed to, uint256 value);15 event Approval(16 address indexed owner,17 address indexed spender,18 uint256 value19 );20}2122// Inline23contract InlineNameSymbol is Dummy {24 // Selector: name() 06fdde0325 function name() public view returns (string memory) {26 require(false, stub_error);27 dummy;28 return "";29 }3031 // Selector: symbol() 95d89b4132 function symbol() public view returns (string memory) {33 require(false, stub_error);34 dummy;35 return "";36 }37}3839// Inline40contract InlineTotalSupply is Dummy {41 // Selector: totalSupply() 18160ddd42 function totalSupply() public view returns (uint256) {43 require(false, stub_error);44 dummy;45 return 0;46 }47}4849contract ERC165 is Dummy {50 // Selector: supportsInterface(bytes4) 01ffc9a751 function supportsInterface(uint32 interfaceId) public view returns (bool) {52 require(false, stub_error);53 interfaceId;54 dummy;55 return false;56 }57}5859contract ERC20 is Dummy, InlineNameSymbol, InlineTotalSupply, ERC20Events {60 // Selector: decimals() 313ce56761 function decimals() public view returns (uint8) {62 require(false, stub_error);63 dummy;64 return 0;65 }6667 // Selector: balanceOf(address) 70a0823168 function balanceOf(address owner) public view returns (uint256) {69 require(false, stub_error);70 owner;71 dummy;72 return 0;73 }7475 // Selector: transfer(address,uint256) a9059cbb76 function transfer(address to, uint256 amount) public returns (bool) {77 require(false, stub_error);78 to;79 amount;80 dummy = 0;81 return false;82 }8384 // Selector: transferFrom(address,address,uint256) 23b872dd85 function transferFrom(86 address from,87 address to,88 uint256 amount89 ) public returns (bool) {90 require(false, stub_error);91 from;92 to;93 amount;94 dummy = 0;95 return false;96 }9798 // Selector: approve(address,uint256) 095ea7b399 function approve(address spender, uint256 amount) public returns (bool) {100 require(false, stub_error);101 spender;102 amount;103 dummy = 0;104 return false;105 }106107 // Selector: allowance(address,address) dd62ed3e108 function allowance(address owner, address spender)109 public110 view111 returns (uint256)112 {113 require(false, stub_error);114 owner;115 spender;116 dummy;117 return 0;118 }119}120121contract UniqueFungible is Dummy, ERC165, ERC20 {}pallets/fungible/src/weights.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/fungible/src/weights.rs
@@ -0,0 +1,31 @@
+#![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 burn_item() -> Weight;
+ fn transfer() -> Weight;
+ fn approve() -> Weight;
+ fn transfer_from() -> Weight;
+}
+
+pub struct SubstrateWeight<T>(PhantomData<T>);
+impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
+ fn create_item() -> Weight {0}
+ fn burn_item() -> Weight {0}
+ fn transfer() -> Weight {0}
+ fn approve() -> Weight {0}
+ fn transfer_from() -> Weight {0}
+}
+
+impl WeightInfo for () {
+ fn create_item() -> Weight {0}
+ fn burn_item() -> Weight {0}
+ fn transfer() -> Weight {0}
+ fn approve() -> Weight {0}
+ fn transfer_from() -> Weight {0}
+}