Skip to content

Commit

Permalink
chore: generic amount type
Browse files Browse the repository at this point in the history
  • Loading branch information
kylezs committed May 6, 2022
1 parent 59a9e9c commit 3e7abd3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
18 changes: 16 additions & 2 deletions state-chain/chains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ pub trait Chain: Member + Parameter {
+ One
+ Saturating
+ From<u64>;

type ChainAmount: Member
+ Parameter
+ Copy
+ Default
+ Into<u128>
+ From<u128>
+ Saturating
+ FullCodec;
}

/// Common crypto-related types and operations for some external chain.
Expand Down Expand Up @@ -126,7 +135,7 @@ pub trait RegisterClaim<Abi: ChainAbi>: ApiCall<Abi> {
}

macro_rules! impl_chains {
( $( $chain:ident { type ChainBlockNumber = $chain_block_number:ty; }, ),+ $(,)? ) => {
( $( $chain:ident { type ChainBlockNumber = $chain_block_number:ty; type ChainAmount = $chain_amount:ty; }, ),+ $(,)? ) => {
use codec::{Decode, Encode};
use sp_runtime::RuntimeDebug;

Expand All @@ -136,13 +145,17 @@ macro_rules! impl_chains {

impl Chain for $chain {
type ChainBlockNumber = $chain_block_number;
type ChainAmount = $chain_amount;
}
)+
};
}

impl_chains! {
Ethereum { type ChainBlockNumber = u64; },
Ethereum {
type ChainBlockNumber = u64;
type ChainAmount = u128;
},
}

impl ChainCrypto for Ethereum {
Expand Down Expand Up @@ -172,6 +185,7 @@ pub mod mocks {
impl_chains! {
MockEthereum {
type ChainBlockNumber = u64;
type ChainAmount = u128;
},
}

Expand Down
16 changes: 11 additions & 5 deletions state-chain/pallets/cf-broadcast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use cf_traits::{
use codec::{Decode, Encode};
use frame_support::{
dispatch::DispatchResultWithPostInfo,
sp_runtime::traits::Saturating,
traits::{Get, OnRuntimeUpgrade, StorageVersion},
Twox64Concat,
};
Expand Down Expand Up @@ -100,6 +101,10 @@ pub mod pallet {
pub type ThresholdSignatureFor<T, I> =
<<T as Config<I>>::TargetChain as ChainCrypto>::ThresholdSignature;

/// Type alias for the Amount type of a particular chain.
pub type ChainAmountFor<T, I> =
<<T as Config<I>>::TargetChain as cf_chains::Chain>::ChainAmount;

#[derive(Clone, RuntimeDebug, PartialEq, Eq, Encode, Decode)]
pub struct BroadcastAttempt<T: Config<I>, I: 'static> {
pub broadcast_attempt_id: BroadcastAttemptId,
Expand Down Expand Up @@ -235,10 +240,11 @@ pub mod pallet {
ValueQuery,
>;

// TODO: Amount type
/// A mapping from signer id to the chain amount (in Atomic units) that the signer is owed
/// for paying transaction fees
#[pallet::storage]
pub type SignerTransactionFeeDeficit<T: Config<I>, I: 'static = ()> =
StorageMap<_, Twox64Concat, SignerIdFor<T, I>, u128, ValueQuery>;
StorageMap<_, Twox64Concat, SignerIdFor<T, I>, ChainAmountFor<T, I>, ValueQuery>;

/// A mapping of signer id to the the account id of the authority that registered the signer.
/// through a transaction_ready_for_transmission extrinsic.
Expand Down Expand Up @@ -404,7 +410,8 @@ pub mod pallet {
{
// Whitelist the signer_id so it can receive fee refunds
if !SignerTransactionFeeDeficit::<T, I>::contains_key(signer_id.clone()) {
SignerTransactionFeeDeficit::<T, I>::insert(signer_id.clone(), 0);
let init_amount: ChainAmountFor<T, I> = Default::default();
SignerTransactionFeeDeficit::<T, I>::insert(signer_id.clone(), init_amount);
SignerIdToAccountId::<T, I>::insert(signer_id, signer);
}

Expand Down Expand Up @@ -564,8 +571,7 @@ pub mod pallet {
tx_signer: SignerIdFor<T, I>,
_block_number: u64,
_tx_hash: TransactionHashFor<T, I>,
// TODO: Amount type
tx_fee: u128,
tx_fee: ChainAmountFor<T, I>,
) -> DispatchResultWithPostInfo {
let _ = T::EnsureWitnessedAtCurrentEpoch::ensure_origin(origin)?;
let broadcast_id = SignatureToBroadcastIdLookup::<T, I>::take(payload)
Expand Down

0 comments on commit 3e7abd3

Please sign in to comment.