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

Commit f8dcb46

Browse files
committed
Make ScalingFactor dynamically adjusted
1 parent 61115de commit f8dcb46

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridges/snowbridge/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridges/snowbridge/pallets/gas-price/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ sp-core = { path = "../../../../substrate/primitives/core", default-features = f
2626
sp-std = { path = "../../../../substrate/primitives/std", default-features = false }
2727
sp-runtime = { path = "../../../../substrate/primitives/runtime", default-features = false }
2828
sp-io = { path = "../../../../substrate/primitives/io", default-features = false, optional = true }
29+
sp-arithmetic = { path = "../../../../substrate/primitives/arithmetic", default-features = false }
2930
snowbridge-core = { path = "../../primitives/core", default-features = false }
3031

3132
[features]
@@ -37,6 +38,7 @@ std = [
3738
"log/std",
3839
"scale-info/std",
3940
"snowbridge-core/std",
41+
"sp-arithmetic/std",
4042
"sp-core/std",
4143
"sp-io/std",
4244
"sp-runtime/std",

bridges/snowbridge/pallets/gas-price/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use frame_support::pallet_prelude::ValueQuery;
1111
use frame_system::WeightInfo;
1212
pub use pallet::*;
1313
use snowbridge_core::{gwei, BaseFeePerGas, GasPriceProvider};
14+
use sp_arithmetic::traits::One;
1415
use sp_core::{Get, U256};
1516
use sp_runtime::{FixedU128, Saturating};
1617

@@ -71,24 +72,32 @@ pub mod pallet {
7172
impl<T: Config> GasPriceProvider for Pallet<T> {
7273
fn update(value: U256, slot: u64) {
7374
let mut accumulated_value: U256 = <AccumulatedGasPrice<T>>::get().value;
75+
let last_updated_slot = <AccumulatedGasPrice<T>>::get().slot;
7476
if accumulated_value.is_zero() {
7577
accumulated_value = DefaultFeePerGas::get();
7678
}
7779

7880
let fixed_value = FixedU128::from_inner(value.low_u128());
7981
let mut accumulated_fixed_value = FixedU128::from_inner(accumulated_value.low_u128());
82+
let scaling_factor = sp_std::cmp::max(
83+
BLENDING_FACTOR,
84+
sp_std::cmp::min(
85+
FixedU128::one(),
86+
FixedU128::from_rational((slot - last_updated_slot).into(), 8192),
87+
),
88+
);
8089

8190
if fixed_value > accumulated_fixed_value {
8291
accumulated_fixed_value = accumulated_fixed_value.saturating_add(
8392
fixed_value
8493
.saturating_sub(accumulated_fixed_value)
85-
.saturating_mul(BLENDING_FACTOR),
94+
.saturating_mul(scaling_factor),
8695
);
8796
} else {
8897
accumulated_fixed_value = accumulated_fixed_value.saturating_sub(
8998
accumulated_fixed_value
9099
.saturating_sub(fixed_value)
91-
.saturating_mul(BLENDING_FACTOR),
100+
.saturating_mul(scaling_factor),
92101
);
93102
}
94103

bridges/snowbridge/pallets/gas-price/src/test.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ fn update_value_works() {
1313
GasPrice::update(gwei(30), 40);
1414
let price = GasPrice::get();
1515
assert_eq!(price, 23440000000_u128.into());
16+
17+
// Update with price decreased, the updated value should be more than the previous but less
18+
// than the new one
1619
GasPrice::update(gwei(20), 50);
1720
let price = GasPrice::get();
1821
assert_eq!(price, 22752000000_u128.into());
22+
23+
// Update with a large interval, the new value should dominate the EMA
24+
GasPrice::update(gwei(30), 50 + 8192);
25+
let price = GasPrice::get();
26+
assert_eq!(price, gwei(30).into());
1927
});
2028
}

0 commit comments

Comments
 (0)