Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
Make ScalingFactor dynamically adjusted
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jun 17, 2024
1 parent 61115de commit f8dcb46
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 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.

1 change: 1 addition & 0 deletions bridges/snowbridge/Cargo.lock

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

2 changes: 2 additions & 0 deletions bridges/snowbridge/pallets/gas-price/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ sp-core = { path = "../../../../substrate/primitives/core", default-features = f
sp-std = { path = "../../../../substrate/primitives/std", default-features = false }
sp-runtime = { path = "../../../../substrate/primitives/runtime", default-features = false }
sp-io = { path = "../../../../substrate/primitives/io", default-features = false, optional = true }
sp-arithmetic = { path = "../../../../substrate/primitives/arithmetic", default-features = false }
snowbridge-core = { path = "../../primitives/core", default-features = false }

[features]
Expand All @@ -37,6 +38,7 @@ std = [
"log/std",
"scale-info/std",
"snowbridge-core/std",
"sp-arithmetic/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
Expand Down
13 changes: 11 additions & 2 deletions bridges/snowbridge/pallets/gas-price/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use frame_support::pallet_prelude::ValueQuery;
use frame_system::WeightInfo;
pub use pallet::*;
use snowbridge_core::{gwei, BaseFeePerGas, GasPriceProvider};
use sp_arithmetic::traits::One;
use sp_core::{Get, U256};
use sp_runtime::{FixedU128, Saturating};

Expand Down Expand Up @@ -71,24 +72,32 @@ pub mod pallet {
impl<T: Config> GasPriceProvider for Pallet<T> {
fn update(value: U256, slot: u64) {
let mut accumulated_value: U256 = <AccumulatedGasPrice<T>>::get().value;
let last_updated_slot = <AccumulatedGasPrice<T>>::get().slot;
if accumulated_value.is_zero() {
accumulated_value = DefaultFeePerGas::get();
}

let fixed_value = FixedU128::from_inner(value.low_u128());
let mut accumulated_fixed_value = FixedU128::from_inner(accumulated_value.low_u128());
let scaling_factor = sp_std::cmp::max(
BLENDING_FACTOR,
sp_std::cmp::min(
FixedU128::one(),
FixedU128::from_rational((slot - last_updated_slot).into(), 8192),
),
);

if fixed_value > accumulated_fixed_value {
accumulated_fixed_value = accumulated_fixed_value.saturating_add(
fixed_value
.saturating_sub(accumulated_fixed_value)
.saturating_mul(BLENDING_FACTOR),
.saturating_mul(scaling_factor),
);
} else {
accumulated_fixed_value = accumulated_fixed_value.saturating_sub(
accumulated_fixed_value
.saturating_sub(fixed_value)
.saturating_mul(BLENDING_FACTOR),
.saturating_mul(scaling_factor),
);
}

Expand Down
8 changes: 8 additions & 0 deletions bridges/snowbridge/pallets/gas-price/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ fn update_value_works() {
GasPrice::update(gwei(30), 40);
let price = GasPrice::get();
assert_eq!(price, 23440000000_u128.into());

// Update with price decreased, the updated value should be more than the previous but less
// than the new one
GasPrice::update(gwei(20), 50);
let price = GasPrice::get();
assert_eq!(price, 22752000000_u128.into());

// Update with a large interval, the new value should dominate the EMA
GasPrice::update(gwei(30), 50 + 8192);
let price = GasPrice::get();
assert_eq!(price, gwei(30).into());
});
}

0 comments on commit f8dcb46

Please sign in to comment.