Skip to content

Commit

Permalink
Merge branch 'main' into phoenix-release
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellam258 committed Apr 29, 2022
2 parents 7c547e4 + c2d44cd commit 6e2f452
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pallets/memo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ frame-system = { default-features = false, git = "https://github.com/BHONetwork/
sp-runtime = { default-features = false, git = "https://github.com/BHONetwork/substrate.git", branch = "bho-polkadot-v0.9.18" }
sp-std = { default-features = false, git = "https://github.com/BHONetwork/substrate.git", branch = "bho-polkadot-v0.9.18" }
pallet-timestamp = { git = "https://github.com/BHONetwork/substrate.git", default-features = false, branch = "bho-polkadot-v0.9.18" }
pallet-transaction-payment = { git = "https://github.com/BHONetwork/substrate.git", default-features = false, branch = "bho-polkadot-v0.9.18" }
common-primitives = { default-features = false, path = '../../primitives/common' }

[dev-dependencies]
Expand Down Expand Up @@ -55,8 +56,10 @@ std = [
'sp-runtime/std',
'common-primitives/std',
'pallet-timestamp/std',
'pallet-transaction-payment/std',
]
try-runtime = [
'frame-support/try-runtime',
'pallet-timestamp/try-runtime',
'pallet-transaction-payment/try-runtime',
]
104 changes: 84 additions & 20 deletions pallets/memo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,18 @@ pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::UnixTime};
use frame_support::{
dispatch::DispatchResult,
pallet_prelude::*,
traits::{Imbalance, IsSubType, UnixTime},
};
use frame_system::pallet_prelude::*;
use pallet_transaction_payment::OnChargeTransaction;
use sp_runtime::traits::{DispatchInfoOf, PostDispatchInfoOf};

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
pub trait Config: frame_system::Config + pallet_transaction_payment::Config {
type UnixTime: UnixTime;

/// Because this pallet emits events, it depends on the runtime's definition of an event.
Expand Down Expand Up @@ -78,7 +84,8 @@ pub mod pallet {

#[pallet::storage]
#[pallet::getter(fn memo_counter)]
pub type MemoCounter<T: Config> = StorageMap<_, Blake2_128Concat, Vec<u8>, u128, ValueQuery>;
pub type MemoCounter<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, u128, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn amount_free_tx)]
Expand Down Expand Up @@ -124,24 +131,12 @@ pub mod pallet {
}
}

impl<T: Config> Pallet<T> {
pub fn calc_actual_weight(sender: &Vec<u8>) -> Pays {
let counter = Pallet::<T>::memo_counter(sender);
if counter < Pallet::<T>::amount_free_tx() {
Pays::No
} else {
Pays::Yes
}
}
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight((T::WeightInfo::create(content.len() as u32, txn_hash.len() as u32),
Pallet::<T>::calc_actual_weight(sender)))]
#[pallet::weight(T::WeightInfo::create(content.len() as u32, txn_hash.len() as u32))]
#[transactional]
pub fn create(
origin: OriginFor<T>,
Expand All @@ -150,7 +145,7 @@ pub mod pallet {
content: Vec<u8>,
sender: Vec<u8>,
receiver: Vec<u8>,
) -> DispatchResult {
) -> DispatchResultWithPostInfo {
let operator = ensure_signed(origin)?;

let memo_info = Memo::<T>::get(&chain_id, &txn_hash);
Expand All @@ -162,17 +157,22 @@ pub mod pallet {
let bounded_content: BoundedVec<u8, T::ContentLimit> =
content.clone().try_into().map_err(|_| Error::<T>::BadMemoInfo)?;

let counter: u128 = MemoCounter::<T>::get(&sender);
let counter: u128 = MemoCounter::<T>::get(&operator);

MemoCounter::<T>::insert(&sender, counter.checked_add(1).unwrap_or_else(Zero::zero));
MemoCounter::<T>::insert(&operator, counter.checked_add(1).unwrap_or_else(Zero::zero));

let memo_info =
MemoInfo { content: bounded_content, sender, receiver, operator, time: time_now };

Memo::<T>::insert(&chain_id, &txn_hash, memo_info.clone());

Self::deposit_event(Event::MemoCreated(chain_id, txn_hash, memo_info));
Ok(())

if counter < Pallet::<T>::amount_free_tx() {
return Ok(Pays::No.into());
}

Ok(().into())
}

// #[pallet::weight(T::WeightInfo::update(content.len() as u32))]
Expand Down Expand Up @@ -208,4 +208,68 @@ pub mod pallet {
Ok(())
}
}

type NegativeImbalanceOf<C, T> =
<C as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;

pub struct MemoOnChargeTransaction<OCT, C>(sp_std::marker::PhantomData<(OCT, C)>);

impl<T, OCT, C> OnChargeTransaction<T> for MemoOnChargeTransaction<OCT, C>
where
T: Config,
T::Call: IsSubType<Call<T>>,
OCT: OnChargeTransaction<
T,
LiquidityInfo = Option<NegativeImbalanceOf<C, T>>,
Balance = <C as Currency<<T as frame_system::Config>::AccountId>>::Balance,
>,
C: Currency<<T as frame_system::Config>::AccountId>,
C::PositiveImbalance: Imbalance<
<C as Currency<<T as frame_system::Config>::AccountId>>::Balance,
Opposite = C::NegativeImbalance,
>,
C::NegativeImbalance: Imbalance<
<C as Currency<<T as frame_system::Config>::AccountId>>::Balance,
Opposite = C::PositiveImbalance,
>,
{
type LiquidityInfo = OCT::LiquidityInfo;
type Balance = OCT::Balance;

fn withdraw_fee(
who: &T::AccountId,
call: &T::Call,
dispatch_info: &DispatchInfoOf<T::Call>,
fee: Self::Balance,
tip: Self::Balance,
) -> Result<Self::LiquidityInfo, TransactionValidityError> {
if let Some(local_call) = call.is_sub_type() {
if let Call::create { .. } = local_call {
let counter = Pallet::<T>::memo_counter(who);
if counter < Pallet::<T>::amount_free_tx() {
return Ok(None);
}
}
}
return OCT::withdraw_fee(who, call, dispatch_info, fee, tip);
}

fn correct_and_deposit_fee(
who: &T::AccountId,
dispatch_info: &DispatchInfoOf<T::Call>,
post_info: &PostDispatchInfoOf<T::Call>,
corrected_fee: Self::Balance,
tip: Self::Balance,
already_withdrawn: Self::LiquidityInfo,
) -> Result<(), TransactionValidityError> {
return OCT::correct_and_deposit_fee(
who,
dispatch_info,
post_info,
corrected_fee,
tip,
already_withdrawn,
);
}
}
}
3 changes: 2 additions & 1 deletion runtime/phoenix/src/pallets/transaction_payment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ parameter_types! {
}

impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = CurrencyAdapter<Balances, DealWithFees>;
type OnChargeTransaction =
bholdus_memo::MemoOnChargeTransaction<CurrencyAdapter<Balances, DealWithFees>, Balances>;
type TransactionByteFee = TransactionByteFee;
type OperationalFeeMultiplier = OperationalFeeMultiplier;
type WeightToFee = fee::WeightToFee;
Expand Down

0 comments on commit 6e2f452

Please sign in to comment.