This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Reward relayer on BH for S->E transfer #162
Closed
yrong
wants to merge
16
commits into
ron/custom-execute-transfer
from
ron/pay-reward-on-source-chain
Closed
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3a79d45
Use low level execute directly
yrong 9401e17
Move remote_fee out from DefaultBridgeHubEthereumBaseFee
yrong b1d88ef
Fix breaking tests
yrong 7a8a3a5
Lock fee asset on BH
yrong de84596
Comment todo
yrong 6ad15ae
Unlock fee and pay reward to relay
yrong aa503e0
Merge branch 'snowbridge' into ron/custom-execute-transfer
yrong 62c0868
Add Payer to westend
yrong e13d082
Fix runtime test
yrong d765b37
Fee paid on BH
yrong 80537ed
Fix breaking tests
yrong 486431d
Merge branch 'ron/custom-execute-transfer' into ron/pay-reward-on-sou…
yrong 3bb7f78
Merge branch 'ron/custom-execute-transfer' into ron/pay-reward-on-sou…
yrong 238d898
Remove the remote fee estimation on chain
yrong eca425f
Lock into Pallet account
yrong c88fe86
Add fees by user origin
yrong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,33 +107,45 @@ use bridge_hub_common::{AggregateMessageOrigin, CustomDigestItem}; | |
use codec::Decode; | ||
use frame_support::{ | ||
storage::StorageStreamIter, | ||
traits::{tokens::Balance, Contains, Defensive, EnqueueMessage, Get, ProcessMessageError}, | ||
traits::{ | ||
fungible::Inspect, tokens::Balance, Contains, Defensive, EnqueueMessage, Get, | ||
ProcessMessageError, | ||
}, | ||
weights::{Weight, WeightToFee}, | ||
}; | ||
pub use pallet::*; | ||
use snowbridge_core::{ | ||
outbound::{Fee, GasMeter, QueuedMessage, VersionedQueuedMessage, ETHER_DECIMALS}, | ||
outbound::{Fee, GasMeter, QueuedMessage, VersionedQueuedMessage}, | ||
BasicOperatingMode, ChannelId, | ||
}; | ||
use snowbridge_outbound_queue_merkle_tree::merkle_root; | ||
pub use snowbridge_outbound_queue_merkle_tree::MerkleProof; | ||
use sp_core::{H256, U256}; | ||
use sp_core::H256; | ||
use sp_runtime::{ | ||
traits::{CheckedDiv, Hash}, | ||
DigestItem, Saturating, | ||
traits::{AccountIdConversion, Hash, Zero}, | ||
DigestItem, | ||
}; | ||
use sp_std::prelude::*; | ||
pub use types::{CommittedMessage, ProcessMessageOriginOf}; | ||
pub use weights::WeightInfo; | ||
|
||
pub use pallet::*; | ||
type BalanceOf<T> = | ||
<<T as pallet::Config>::Token as Inspect<<T as frame_system::Config>::AccountId>>::Balance; | ||
|
||
#[frame_support::pallet] | ||
pub mod pallet { | ||
use super::*; | ||
use frame_support::pallet_prelude::*; | ||
use frame_support::{ | ||
pallet_prelude::*, | ||
traits::{ | ||
fungible::{Inspect, Mutate}, | ||
tokens::Preservation, | ||
}, | ||
PalletId, | ||
}; | ||
use frame_system::pallet_prelude::*; | ||
use snowbridge_core::PricingParameters; | ||
use sp_arithmetic::FixedU128; | ||
use snowbridge_core::{PayRewardError, PricingParameters}; | ||
use sp_arithmetic::traits::SaturatedConversion; | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
@@ -173,6 +185,11 @@ pub mod pallet { | |
|
||
/// Weight information for extrinsics in this pallet | ||
type WeightInfo: WeightInfo; | ||
|
||
/// Message relayers are rewarded with this asset | ||
type Token: Mutate<Self::AccountId> + Inspect<Self::AccountId>; | ||
|
||
type PotId: Get<PalletId>; | ||
} | ||
|
||
#[pallet::event] | ||
|
@@ -243,6 +260,10 @@ pub mod pallet { | |
#[pallet::getter(fn operating_mode)] | ||
pub type OperatingMode<T: Config> = StorageValue<_, BasicOperatingMode, ValueQuery>; | ||
|
||
/// Fee locked by message hash | ||
#[pallet::storage] | ||
pub type LockedFee<T: Config> = StorageMap<_, Twox64Concat, H256, u128, ValueQuery>; | ||
|
||
#[pallet::hooks] | ||
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> | ||
where | ||
|
@@ -280,6 +301,18 @@ pub mod pallet { | |
Self::deposit_event(Event::OperatingModeChanged { mode }); | ||
Ok(()) | ||
} | ||
|
||
#[pallet::call_index(1)] | ||
#[pallet::weight({100_000})] | ||
pub fn add_fees( | ||
origin: OriginFor<T>, | ||
message_id: H256, | ||
fee_amount: BalanceOf<T>, | ||
) -> DispatchResult { | ||
ensure_signed(origin)?; | ||
Self::lock_fee(message_id, fee_amount)?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
|
@@ -374,39 +407,10 @@ pub mod pallet { | |
/// Calculate total fee in native currency to cover all costs of delivering a message to the | ||
/// remote destination. See module-level documentation for more details. | ||
pub(crate) fn calculate_fee( | ||
gas_used_at_most: u64, | ||
params: PricingParameters<T::Balance>, | ||
_gas_used_at_most: u64, | ||
_params: PricingParameters<T::Balance>, | ||
) -> Fee<T::Balance> { | ||
// Remote fee in ether | ||
let fee = Self::calculate_remote_fee( | ||
gas_used_at_most, | ||
params.fee_per_gas, | ||
params.rewards.remote, | ||
); | ||
|
||
// downcast to u128 | ||
let fee: u128 = fee.try_into().defensive_unwrap_or(u128::MAX); | ||
|
||
// multiply by multiplier and convert to local currency | ||
let fee = FixedU128::from_inner(fee) | ||
.saturating_mul(params.multiplier) | ||
.checked_div(¶ms.exchange_rate) | ||
.expect("exchange rate is not zero; qed") | ||
.into_inner(); | ||
|
||
// adjust fixed point to match local currency | ||
let fee = Self::convert_from_ether_decimals(fee); | ||
|
||
Fee::from((Self::calculate_local_fee(), fee)) | ||
} | ||
|
||
/// Calculate fee in remote currency for dispatching a message on Ethereum | ||
pub(crate) fn calculate_remote_fee( | ||
gas_used_at_most: u64, | ||
fee_per_gas: U256, | ||
reward: U256, | ||
) -> U256 { | ||
fee_per_gas.saturating_mul(gas_used_at_most.into()).saturating_add(reward) | ||
Fee::from((Self::calculate_local_fee(), T::Balance::zero())) | ||
Comment on lines
409
to
+413
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will remove price params on chain and the remote portion of the fee. |
||
} | ||
|
||
/// The local component of the message processing fees in native currency | ||
|
@@ -416,13 +420,38 @@ pub mod pallet { | |
) | ||
} | ||
|
||
// 1 DOT has 10 digits of precision | ||
// 1 KSM has 12 digits of precision | ||
// 1 ETH has 18 digits of precision | ||
pub(crate) fn convert_from_ether_decimals(value: u128) -> T::Balance { | ||
let decimals = ETHER_DECIMALS.saturating_sub(T::Decimals::get()) as u32; | ||
let denom = 10u128.saturating_pow(decimals); | ||
value.checked_div(denom).expect("divisor is non-zero; qed").into() | ||
pub fn account_id() -> T::AccountId { | ||
T::PotId::get().into_account_truncating() | ||
} | ||
|
||
pub(crate) fn lock_fee(message_id: H256, fee_amount: BalanceOf<T>) -> DispatchResult { | ||
T::Token::mint_into(&Self::account_id(), fee_amount)?; | ||
<LockedFee<T>>::try_mutate(message_id, |amount| -> DispatchResult { | ||
*amount = amount.saturating_add(fee_amount.saturated_into::<u128>()); | ||
Ok(()) | ||
})?; | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn unlock_fee( | ||
message_id: H256, | ||
beneficiary: [u8; 32], | ||
) -> Result<(), PayRewardError> | ||
where | ||
<T as frame_system::Config>::AccountId: From<[u8; 32]>, | ||
{ | ||
let amount: BalanceOf<T> = <LockedFee<T>>::get(message_id).saturated_into(); | ||
|
||
T::Token::transfer( | ||
&Self::account_id(), | ||
&beneficiary.into(), | ||
amount, | ||
Preservation::Preserve, | ||
) | ||
.map_err(|_| PayRewardError::UnlockFailed)?; | ||
|
||
<LockedFee<T>>::remove(message_id); | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The storage map with Fee(in DOT) locked by MessageId.