difftreelog
refactor switch charging logic to sponsoring primitive
in: master
6 files changed
pallets/nft-charge-transaction/Cargo.tomldiffbeforeafterboth--- a/pallets/nft-charge-transaction/Cargo.toml
+++ b/pallets/nft-charge-transaction/Cargo.toml
@@ -23,19 +23,14 @@
frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
pallet-balances = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-contracts = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-timestamp = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
pallet-transaction-payment = { default-features = false, version = "3.0.0", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-randomness-collective-flip = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-std = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
frame-benchmarking = { default-features = false, version = "3.0.0", optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-core = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-io = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-runtime = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-nft = { default-features = false, path="../nft" }
pallet-nft-transaction-payment = { default-features = false, path="../nft-transaction-payment" }
-nft-data-structs = { default-features = false, path="../../primitives", version = "0.9.0" }
[features]
default = ['std']
@@ -45,15 +40,10 @@
'frame-support/std',
'frame-system/std',
'pallet-balances/std',
- 'pallet-timestamp/std',
- 'pallet-randomness-collective-flip/std',
- 'pallet-contracts/std',
- 'pallet-nft/std',
'pallet-transaction-payment/std',
'pallet-nft-transaction-payment/std',
'sp-std/std',
'sp-runtime/std',
- 'nft-data-structs/std',
'frame-benchmarking/std',
]
runtime-benchmarks = ["frame-benchmarking"]
pallets/nft-charge-transaction/src/lib.rsdiffbeforeafterboth--- a/pallets/nft-charge-transaction/src/lib.rs
+++ b/pallets/nft-charge-transaction/src/lib.rs
@@ -14,16 +14,10 @@
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
-#[cfg(test)]
-mod tests;
-
use codec::{Decode, Encode};
-use frame_support::traits::{ Get};
+use frame_support::traits::Get;
use frame_support::{
decl_module, decl_storage,
- traits::{
- IsSubType,
- },
weights::{
DispatchInfo, PostDispatchInfo, DispatchClass
}
@@ -37,7 +31,6 @@
},
FixedPointOperand, DispatchResult
};
-use pallet_contracts::chain_extension::UncheckedFrom;
use pallet_transaction_payment::OnChargeTransaction;
use sp_std::prelude::*;
@@ -83,10 +76,8 @@
impl<T: Config> ChargeTransactionPayment<T>
where
- T::Call: Dispatchable<Info=DispatchInfo, PostInfo=PostDispatchInfo> + IsSubType<pallet_nft::Call<T>> + IsSubType<pallet_contracts::Call<T>>,
+ T::Call: Dispatchable<Info=DispatchInfo, PostInfo=PostDispatchInfo>,
BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,
- T::AccountId: AsRef<[u8]>,
- T::AccountId: UncheckedFrom<T::Hash>,
{
fn traditional_fee(
len: usize,
@@ -134,13 +125,6 @@
.map(|i| (fee, i));
}
- // check errors
- let _error = <pallet_nft_transaction_payment::Module<T>>::check_error(who, call);
- match _error {
- Err(_error) => return Err(_error),
- Ok(_error) => {}
- };
-
// Determine who is paying transaction fee based on ecnomic model
// Parse call to extract collection ID and access collection sponsor
let sponsor = <pallet_nft_transaction_payment::Module<T>>::withdraw_type(who, call);
@@ -156,9 +140,7 @@
for ChargeTransactionPayment<T>
where
BalanceOf<T>: Send + Sync + From<u64> + FixedPointOperand,
- T::Call: Dispatchable<Info=DispatchInfo, PostInfo=PostDispatchInfo> + IsSubType<pallet_nft::Call<T>> + IsSubType<pallet_contracts::Call<T>>,
- T::AccountId: AsRef<[u8]>,
- T::AccountId: UncheckedFrom<T::Hash>,
+ T::Call: Dispatchable<Info=DispatchInfo, PostInfo=PostDispatchInfo>,
{
const IDENTIFIER: &'static str = "ChargeTransactionPayment";
type AccountId = T::AccountId;
@@ -209,7 +191,7 @@
_result: &DispatchResult,
) -> Result<(), TransactionValidityError> {
let (tip, who, imbalance) = pre;
- let actual_fee = pallet_transaction_payment::Module::<T>::compute_actual_fee(
+ let actual_fee = pallet_transaction_payment::Pallet::<T>::compute_actual_fee(
len as u32,
info,
post_info,
pallets/nft-transaction-payment/Cargo.tomldiffbeforeafterboth--- a/pallets/nft-transaction-payment/Cargo.toml
+++ b/pallets/nft-transaction-payment/Cargo.toml
@@ -22,19 +22,14 @@
serde = { version = "1.0.119", default-features = false }
frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-balances = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-contracts = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-timestamp = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
pallet-transaction-payment = { default-features = false, version = "3.0.0", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-randomness-collective-flip = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-std = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
frame-benchmarking = { default-features = false, version = "3.0.0", optional = true, git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-core = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-io = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
sp-runtime = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.3' }
-pallet-nft = { default-features = false, path="../nft" }
-nft-data-structs = { default-features = false, path="../../primitives", version = "0.9.0" }
+up-sponsorship = { default-features = false, path = "../../primitives/sponsorship", version = "0.1.0" }
[features]
default = ['std']
@@ -43,15 +38,13 @@
'serde/std',
'frame-support/std',
'frame-system/std',
- 'pallet-balances/std',
- 'pallet-timestamp/std',
- 'pallet-randomness-collective-flip/std',
- 'pallet-contracts/std',
- 'pallet-nft/std',
+ 'sp-core/std',
+ 'sp-io/std',
'pallet-transaction-payment/std',
'sp-std/std',
'sp-runtime/std',
- 'nft-data-structs/std',
'frame-benchmarking/std',
+
+ 'up-sponsorship/std',
]
runtime-benchmarks = ["frame-benchmarking"]
pallets/nft-transaction-payment/src/benchmarking.rsdiffbeforeafterboth--- a/pallets/nft-transaction-payment/src/benchmarking.rs
+++ b/pallets/nft-transaction-payment/src/benchmarking.rs
@@ -1,7 +1,6 @@
#![cfg(feature = "runtime-benchmarks")]
use super::*;
-use crate::Module as NftTransactionPayment;
use sp_std::prelude::*;
use frame_system::RawOrigin;
pallets/nft-transaction-payment/src/lib.rsdiffbeforeafterboth--- a/pallets/nft-transaction-payment/src/lib.rs
+++ b/pallets/nft-transaction-payment/src/lib.rs
@@ -14,388 +14,32 @@
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
-#[cfg(test)]
-mod tests;
-
-use frame_support::{
- decl_error, decl_module, decl_storage,
- traits::{
- IsSubType,
- },
- weights::{
- DispatchInfo
- }
-};
-use sp_runtime::traits::StaticLookup;
-use sp_runtime::{
- traits::{
- Hash, Dispatchable,
- },
- transaction_validity::{
- InvalidTransaction, TransactionValidityError,
- },
-};
-use pallet_contracts::chain_extension::UncheckedFrom;
+use frame_support::{decl_module, decl_storage};
use sp_std::prelude::*;
-use nft_data_structs::{
- CreateItemData,
- CollectionId, CollectionMode, TokenId
-};
-
-type CodeHash<T> = <T as frame_system::Config>::Hash;
+use up_sponsorship::SponsorshipHandler;
- pub trait Config: frame_system::Config + pallet_contracts::Config + pallet_transaction_payment::Config + pallet_nft::Config {
- }
-
- // Error for non-fungible-token module.
-
- decl_error! {
- /// Error for non-fungible-token module.
- pub enum Error for Module<T: Config> {
- /// No available class ID
- NoAvailableClassId,
- /// No available token ID
- NoAvailableTokenId,
- /// Token(ClassId, TokenId) not found
- TokenNotFound,
- /// Class not found
- CollectionNotFound,
- /// The operator is not the owner of the token and has no permission
- NoPermission,
- /// Arithmetic calculation overflow
- NumOverflow,
- /// Can not destroy class
- /// Total issuance is not 0
- CannotDestroyClass,
- }
+pub trait Config: frame_system::Config + pallet_transaction_payment::Config {
+ type SponsorshipHandler: SponsorshipHandler<Self::AccountId, Self::Call>;
}
-
- decl_storage! {
- trait Store for Module<T: Config> as NftTransactionPayment{
+decl_storage! {
+ trait Store for Module<T: Config> as NftTransactionPayment{
}
}
decl_module! {
-
pub struct Module<T: Config> for enum Call
where
origin: T::Origin,
{
}
}
-
-impl<T: Config> Module<T>
-{
- pub fn check_error(
- who: &T::AccountId,
- call: &T::Call
- ) -> Result<bool, TransactionValidityError> where
- T::Call: Dispatchable<Info=DispatchInfo>,
- T::Call: IsSubType<pallet_nft::Call<T>>,
- T::Call: IsSubType<pallet_contracts::Call<T>>,
- T::AccountId: AsRef<[u8]>,
- T::AccountId: UncheckedFrom<T::Hash>
- {
-
- match IsSubType::<pallet_contracts::Call<T>>::is_sub_type(call) {
- Some(pallet_contracts::Call::call(dest, _value, _gas_limit, _data)) => {
-
- let called_contract: T::AccountId = T::Lookup::lookup((*dest).clone()).unwrap_or(T::AccountId::default());
-
- let owned_contract = pallet_nft::ContractOwner::<T>::get(called_contract.clone()).as_ref() == Some(who);
- let white_list_enabled = pallet_nft::ContractWhiteListEnabled::<T>::contains_key(called_contract.clone());
-
- if !owned_contract && white_list_enabled {
- if !pallet_nft::ContractWhiteList::<T>::contains_key(called_contract.clone(), who) {
- return Err(InvalidTransaction::Call.into());
- }
- }
- Ok(true)
- },
- _ => { Ok(true) },
- }
- }
-
+impl<T: Config> Module<T> {
pub fn withdraw_type(
who: &T::AccountId,
call: &T::Call
- ) -> Option<T::AccountId> where
- T::Call: Dispatchable<Info=DispatchInfo>,
- T::Call: IsSubType<pallet_nft::Call<T>>,
- T::Call: IsSubType<pallet_contracts::Call<T>>,
- T::AccountId: AsRef<[u8]>,
- T::AccountId: UncheckedFrom<T::Hash>
- {
-
- let mut sponsor: Option<T::AccountId> = match IsSubType::<pallet_nft::Call<T>>::is_sub_type(call) {
- Some(pallet_nft::Call::create_item(collection_id, _owner, _properties)) => {
-
- Self::withdraw_create_item(who, collection_id, &_properties)
- },
- Some(pallet_nft::Call::transfer(_new_owner, collection_id, item_id, _value)) => {
-
- Self::withdraw_transfer(who, collection_id, item_id)
- },
- Some(pallet_nft::Call::set_variable_meta_data(collection_id, item_id, data)) => {
-
- Self::withdraw_set_variable_meta_data(collection_id, item_id, &data)
- },
- _ => None,
- };
-
- sponsor = sponsor.or_else(|| match IsSubType::<pallet_contracts::Call<T>>::is_sub_type(call) {
- Some(pallet_contracts::Call::call(dest, _value, _gas_limit, _data)) => {
-
- Self::withdraw_contract_call(who, dest)
- },
- Some(pallet_contracts::Call::instantiate(_endowment, _gas_limit, code_hash, _data, salt)) => {
-
- Self::withdraw_contract_instantiate(&who, code_hash, salt)
- },
- Some(pallet_contracts::Call::instantiate_with_code(_endowment, _gas_limit, _code, _data, _salt)) => {
-
- Self::withdraw_contract_instantiate(&who, &T::Hashing::hash(&_code), _salt)
- },
- _ => None,
- });
-
- sponsor
- }
-
-
-
- pub fn withdraw_create_item(
- who: &T::AccountId,
- collection_id: &CollectionId,
- _properties: &CreateItemData,
) -> Option<T::AccountId> {
-
- let collection = pallet_nft::CollectionById::<T>::get(collection_id)?;
-
- // sponsor timeout
- let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
-
- let limit = collection.limits.sponsor_transfer_timeout;
- if pallet_nft::CreateItemBasket::<T>::contains_key((collection_id, &who)) {
- let last_tx_block = pallet_nft::CreateItemBasket::<T>::get((collection_id, &who));
- let limit_time = last_tx_block + limit.into();
- if block_number <= limit_time {
- return None;
- }
- }
- pallet_nft::CreateItemBasket::<T>::insert((collection_id, who.clone()), block_number);
-
- // check free create limit
- if collection.limits.sponsored_data_size >= (_properties.len() as u32) {
- collection.sponsorship.sponsor()
- .cloned()
- } else {
- None
- }
- }
-
- pub fn withdraw_transfer(
- who: &T::AccountId,
- collection_id: &CollectionId,
- item_id: &TokenId,
- ) -> Option<T::AccountId> {
-
- let collection = pallet_nft::CollectionById::<T>::get(collection_id)?;
- let limits = pallet_nft::ChainLimit::get();
-
- let mut sponsor_transfer = false;
- if collection.sponsorship.confirmed() {
-
- let collection_limits = collection.limits.clone();
- let collection_mode = collection.mode.clone();
-
- // sponsor timeout
- let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
- sponsor_transfer = match collection_mode {
- CollectionMode::NFT => {
-
- // get correct limit
- let limit: u32 = if collection_limits.sponsor_transfer_timeout > 0 {
- collection_limits.sponsor_transfer_timeout
- } else {
- limits.nft_sponsor_transfer_timeout
- };
-
- let mut sponsored = true;
- if pallet_nft::NftTransferBasket::<T>::contains_key(collection_id, item_id) {
- let last_tx_block = pallet_nft::NftTransferBasket::<T>::get(collection_id, item_id);
- let limit_time = last_tx_block + limit.into();
- if block_number <= limit_time {
- sponsored = false;
- }
- }
- if sponsored {
- pallet_nft::NftTransferBasket::<T>::insert(collection_id, item_id, block_number);
- }
-
- sponsored
- }
- CollectionMode::Fungible(_) => {
-
- // get correct limit
- let limit: u32 = if collection_limits.sponsor_transfer_timeout > 0 {
- collection_limits.sponsor_transfer_timeout
- } else {
- limits.fungible_sponsor_transfer_timeout
- };
-
- let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
- let mut sponsored = true;
- if pallet_nft::FungibleTransferBasket::<T>::contains_key(collection_id, who) {
- let last_tx_block = pallet_nft::FungibleTransferBasket::<T>::get(collection_id, who);
- let limit_time = last_tx_block + limit.into();
- if block_number <= limit_time {
- sponsored = false;
- }
- }
- if sponsored {
- pallet_nft::FungibleTransferBasket::<T>::insert(collection_id, who, block_number);
- }
-
- sponsored
- }
- CollectionMode::ReFungible => {
-
- // get correct limit
- let limit: u32 = if collection_limits.sponsor_transfer_timeout > 0 {
- collection_limits.sponsor_transfer_timeout
- } else {
- limits.refungible_sponsor_transfer_timeout
- };
-
- let mut sponsored = true;
- if pallet_nft::ReFungibleTransferBasket::<T>::contains_key(collection_id, item_id) {
- let last_tx_block = pallet_nft::ReFungibleTransferBasket::<T>::get(collection_id, item_id);
- let limit_time = last_tx_block + limit.into();
- if block_number <= limit_time {
- sponsored = false;
- }
- }
- if sponsored {
- pallet_nft::ReFungibleTransferBasket::<T>::insert(collection_id, item_id, block_number);
- }
-
- sponsored
- }
- _ => {
- false
- },
- };
- }
-
- if !sponsor_transfer {
- None
- } else {
- collection.sponsorship.sponsor()
- .cloned()
- }
- }
-
- pub fn withdraw_set_variable_meta_data(
- collection_id: &CollectionId,
- item_id: &TokenId,
- data: &Vec<u8>,
- ) -> Option<T::AccountId> {
-
- let mut sponsor_metadata_changes = false;
-
- let collection = pallet_nft::CollectionById::<T>::get(collection_id)?;
-
- if
- collection.sponsorship.confirmed() &&
- // Can't sponsor fungible collection, this tx will be rejected
- // as invalid
- !matches!(collection.mode, CollectionMode::Fungible(_)) &&
- data.len() <= collection.limits.sponsored_data_size as usize
- {
- if let Some(rate_limit) = collection.limits.sponsored_data_rate_limit {
- let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
-
- if pallet_nft::VariableMetaDataBasket::<T>::get(collection_id, item_id)
- .map(|last_block| block_number - last_block > rate_limit)
- .unwrap_or(true)
- {
- sponsor_metadata_changes = true;
- pallet_nft::VariableMetaDataBasket::<T>::insert(collection_id, item_id, block_number);
- }
- }
- }
-
- if !sponsor_metadata_changes {
- None
- } else {
- collection.sponsorship.sponsor().cloned()
- }
-
- }
-
- pub fn withdraw_contract_call(
- who: &T::AccountId,
- dest: &<T::Lookup as StaticLookup>::Source
- ) -> Option<T::AccountId> {
-
- let called_contract: T::AccountId = T::Lookup::lookup((dest).clone()).unwrap_or(T::AccountId::default());
-
- let owned_contract = pallet_nft::ContractOwner::<T>::get(called_contract.clone()).as_ref() == Some(who);
- let white_list_enabled = pallet_nft::ContractWhiteListEnabled::<T>::contains_key(called_contract.clone());
-
- // ???
- if !owned_contract && white_list_enabled {
- if !pallet_nft::ContractWhiteList::<T>::contains_key(called_contract.clone(), who) {
- return Some(who.clone())
- // return Err(InvalidTransaction::Call.into());
- }
- }
-
- let mut sponsor_transfer = false;
- if pallet_nft::ContractSponsoringRateLimit::<T>::contains_key(called_contract.clone()) {
- let last_tx_block = pallet_nft::ContractSponsorBasket::<T>::get((&called_contract, &who));
- let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
- let rate_limit = pallet_nft::ContractSponsoringRateLimit::<T>::get(&called_contract);
- let limit_time = last_tx_block + rate_limit;
-
- if block_number >= limit_time {
- pallet_nft::ContractSponsorBasket::<T>::insert((called_contract.clone(), who.clone()), block_number);
- sponsor_transfer = true;
- }
- } else {
- sponsor_transfer = false;
- }
-
- if sponsor_transfer {
- if pallet_nft::ContractSelfSponsoring::<T>::contains_key(called_contract.clone()) {
- if pallet_nft::ContractSelfSponsoring::<T>::get(called_contract.clone()) {
- return Some(called_contract);
- }
- }
- }
-
- None
- }
-
- pub fn withdraw_contract_instantiate(
- who: &T::AccountId,
- code_hash: &CodeHash<T>,
- salt: &[u8],
- ) -> Option<T::AccountId> where
- T::AccountId: AsRef<[u8]>,
- T::AccountId: UncheckedFrom<T::Hash>
- {
-
- let new_contract_address = <pallet_contracts::Pallet<T>>::contract_address(
- &who,
- code_hash,
- salt,
- );
- pallet_nft::ContractOwner::<T>::insert(new_contract_address.clone(), who.clone());
-
- None
+ T::SponsorshipHandler::get_sponsor(who, call)
}
}
\ No newline at end of file
pallets/nft-transaction-payment/src/tests.rsdiffbeforeafterbothno changes