Improve fees module (#1821)

* remove amount associated
* make a new trait to bound some arithmetics to balances or assets:
  It also remove arithmetic bounds of srml-support::traits::Currency.

  To update your code then use srml_support::traits::ArithmeticType like:
  `type Currency: ArithmeticType + Currency<Self::AccountId, Balance=BalanceOf<Self>>; ` 
  with `type BalanceOf<T> = <<T as Trait>::Currency as ArithmeticType>::Type; `

* improve decl_storage when it explicit serde bound: basically don't try to be smarter than rust and just use where clause.
This commit is contained in:
thiolliere
2019-02-19 17:13:59 +01:00
committed by GitHub
parent 9a2f1b2007
commit 8065116ba5
16 changed files with 48 additions and 100 deletions
+13 -14
View File
@@ -19,9 +19,9 @@
// Ensure we're `no_std` when compiling for Wasm.
#![cfg_attr(not(feature = "std"), no_std)]
use srml_support::{dispatch::Result, Parameter, StorageMap, decl_event, decl_storage, decl_module};
use srml_support::{dispatch::Result, traits::ArithmeticType, StorageMap, decl_event, decl_storage, decl_module};
use runtime_primitives::traits::{
As, Member, SimpleArithmetic, ChargeBytesFee, ChargeFee,
As, ChargeBytesFee, ChargeFee,
TransferAsset, CheckedAdd, CheckedSub, CheckedMul, Zero
};
use system;
@@ -29,15 +29,14 @@ use system;
mod mock;
mod tests;
type AssetOf<T> = <<T as Trait>::TransferAsset as ArithmeticType>::Type;
pub trait Trait: system::Trait {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
/// The unit for fee amount
type Amount: Member + Parameter + SimpleArithmetic + Default + Copy + As<u64>;
/// A function does the asset transfer between accounts
type TransferAsset: TransferAsset<Self::AccountId, Amount = Self::Amount>;
type TransferAsset: ArithmeticType + TransferAsset<Self::AccountId, Amount=AssetOf<Self>>;
}
decl_module! {
@@ -58,7 +57,7 @@ decl_module! {
}
decl_event!(
pub enum Event<T> where <T as Trait>::Amount {
pub enum Event<T> where Amount = AssetOf<T> {
/// Fee charged (extrinsic_index, fee_amount)
Charged(u32, Amount),
}
@@ -67,22 +66,22 @@ decl_event!(
decl_storage! {
trait Store for Module<T: Trait> as Fees {
/// The fee to be paid for making a transaction; the base.
pub TransactionBaseFee get(transaction_base_fee) config(): T::Amount;
pub TransactionBaseFee get(transaction_base_fee) config(): AssetOf<T>;
/// The fee to be paid for making a transaction; the per-byte portion.
pub TransactionByteFee get(transaction_byte_fee) config(): T::Amount;
pub TransactionByteFee get(transaction_byte_fee) config(): AssetOf<T>;
/// The `extrinsic_index => accumulated_fees` map, containing records to
/// track the overall charged fees for each transaction.
///
/// All records should be removed at finalise stage.
CurrentTransactionFee get(current_transaction_fee): map u32 => T::Amount;
CurrentTransactionFee get(current_transaction_fee): map u32 => AssetOf<T>;
}
}
impl<T: Trait> ChargeBytesFee<T::AccountId> for Module<T> {
fn charge_base_bytes_fee(transactor: &T::AccountId, encoded_len: usize) -> Result {
let bytes_fee = Self::transaction_byte_fee().checked_mul(
&<T::Amount as As<u64>>::sa(encoded_len as u64)
&<AssetOf<T> as As<u64>>::sa(encoded_len as u64)
).ok_or_else(|| "bytes fee overflow")?;
let overall = Self::transaction_base_fee().checked_add(&bytes_fee).ok_or_else(|| "bytes fee overflow")?;
Self::charge_fee(transactor, overall)
@@ -90,9 +89,9 @@ impl<T: Trait> ChargeBytesFee<T::AccountId> for Module<T> {
}
impl<T: Trait> ChargeFee<T::AccountId> for Module<T> {
type Amount = T::Amount;
type Amount = AssetOf<T>;
fn charge_fee(transactor: &T::AccountId, amount: T::Amount) -> Result {
fn charge_fee(transactor: &T::AccountId, amount: AssetOf<T>) -> Result {
let extrinsic_index = <system::Module<T>>::extrinsic_index().ok_or_else(|| "no extrinsic index found")?;
let current_fee = Self::current_transaction_fee(extrinsic_index);
let new_fee = current_fee.checked_add(&amount).ok_or_else(|| "fee got overflow after charge")?;
@@ -103,7 +102,7 @@ impl<T: Trait> ChargeFee<T::AccountId> for Module<T> {
Ok(())
}
fn refund_fee(transactor: &T::AccountId, amount: T::Amount) -> Result {
fn refund_fee(transactor: &T::AccountId, amount: AssetOf<T>) -> Result {
let extrinsic_index = <system::Module<T>>::extrinsic_index().ok_or_else(|| "no extrinsic index found")?;
let current_fee = Self::current_transaction_fee(extrinsic_index);
let new_fee = current_fee.checked_sub(&amount).ok_or_else(|| "fee got underflow after refund")?;
+6 -3
View File
@@ -25,7 +25,7 @@ use runtime_primitives::{
};
use primitives::{H256, Blake2Hasher};
use runtime_io;
use srml_support::{impl_outer_origin, impl_outer_event};
use srml_support::{impl_outer_origin, impl_outer_event, traits::ArithmeticType};
use crate::{GenesisConfig, Module, Trait, system};
impl_outer_origin!{
@@ -52,6 +52,10 @@ impl<AccountId> TransferAsset<AccountId> for TransferAssetMock {
fn add_to(_: &AccountId, _: Self::Amount) -> Result<(), &'static str> { Ok(()) }
}
impl ArithmeticType for TransferAssetMock {
type Type = u64;
}
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Test;
@@ -69,8 +73,7 @@ impl system::Trait for Test {
type Log = DigestItem;
}
impl Trait for Test {
type Amount = u64;
type Event =TestEvent;
type Event = TestEvent;
type TransferAsset = TransferAssetMock;
}