Skip to content

Commit

Permalink
Merge pull request #1016 from galacticcouncil/feat/bifrost-oracle-sup…
Browse files Browse the repository at this point in the history
…port

feat: support to receive bifrost oracle updates
  • Loading branch information
enthusiastmartin authored Feb 8, 2025
2 parents 47e2b7f + 9d784de commit 6228871
Show file tree
Hide file tree
Showing 14 changed files with 85 additions and 16 deletions.
11 changes: 6 additions & 5 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion pallets/dca/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = 'pallet-dca'
version = "1.8.1"
version = "1.8.2"
description = 'A pallet to manage DCA scheduling'
authors = ['GalacticCouncil']
edition = '2021'
Expand Down
9 changes: 8 additions & 1 deletion pallets/dca/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use crate as dca;
use crate::{Config, Error, RandomnessProvider, RelayChainBlockHashProvider};
use cumulus_primitives_core::relay_chain::Hash;
use frame_support::traits::{Everything, Nothing};
use frame_support::traits::{Everything, Nothing, SortedMembers};
use frame_support::weights::constants::ExtrinsicBaseWeight;
use frame_support::weights::WeightToFeeCoefficient;
use frame_support::weights::{IdentityFee, Weight};
Expand Down Expand Up @@ -141,6 +141,12 @@ parameter_types! {
OraclePeriod::LastBlock, OraclePeriod::Short, OraclePeriod::TenMinutes]);
}

pub struct BifrostAcc;
impl SortedMembers<AccountId> for BifrostAcc {
fn sorted_members() -> Vec<AccountId> {
return vec![ALICE];
}
}
impl pallet_ema_oracle::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityOrigin = EnsureRoot<AccountId>;
Expand All @@ -150,6 +156,7 @@ impl pallet_ema_oracle::Config for Test {
type MaxUniqueEntries = ConstU32<20>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
type BifrostOrigin = frame_system::EnsureSignedBy<BifrostAcc, AccountId>;
type WeightInfo = ();
}

Expand Down
5 changes: 4 additions & 1 deletion pallets/ema-oracle/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-ema-oracle"
version = "1.3.4"
version = "1.4.0"
description = "Exponential moving average oracle for AMM pools"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down Expand Up @@ -28,6 +28,8 @@ sp-core = { workspace = true }
sp-std = { workspace = true }
sp-runtime = { workspace = true }

polkadot-xcm = { workspace = true }

log = { workspace = true }

[dev-dependencies]
Expand Down Expand Up @@ -57,5 +59,6 @@ std = [
"hydradx-traits/std",
"hydra-dx-math/std",
"scale-info/std",
"polkadot-xcm/std",
]
try-runtime = ["frame-support/try-runtime"]
16 changes: 16 additions & 0 deletions pallets/ema-oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ pub mod pallet {
/// Origin that can enable oracle for assets that would be rejected by `OracleWhitelist` otherwise.
type AuthorityOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Origin that can update bifrost oracle via `update_bifrost_oracle` extrinsic.
type BifrostOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Provider for the current block number.
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;

Expand Down Expand Up @@ -289,6 +292,19 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(<T as Config>::WeightInfo::update_bifrost_oracle())]
pub fn update_bifrost_oracle(
origin: OriginFor<T>,
//NOTE: these must be boxed becasue of https://github.com/paritytech/polkadot-sdk/blob/6875d36b2dba537f3254aad3db76ac7aa656b7ab/substrate/frame/utility/src/lib.rs#L150
_asset_a: Box<polkadot_xcm::VersionedLocation>,
_asset_b: Box<polkadot_xcm::VersionedLocation>,
_price: (Balance, Balance),
) -> DispatchResult {
T::BifrostOrigin::ensure_origin(origin)?;
Ok(())
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion pallets/ema-oracle/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use frame_support::sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
use frame_support::traits::{Contains, Everything};
use frame_support::traits::{Contains, Everything, SortedMembers};
use frame_support::BoundedVec;
use frame_system::EnsureRoot;
use hydradx_traits::OraclePeriod::{self, *};
Expand All @@ -41,6 +41,8 @@ type Block = frame_system::mocking::MockBlock<Test>;

use crate::MAX_PERIODS;

pub const ALICE: AccountId = 1;

pub const HDX: AssetId = 1_000;
pub const DOT: AssetId = 2_000;
pub const ACA: AssetId = 3_000;
Expand Down Expand Up @@ -139,6 +141,12 @@ impl Contains<(Source, AssetId, AssetId)> for OracleWhitelist {
}
}

pub struct BifrostAcc;
impl SortedMembers<AccountId> for BifrostAcc {
fn sorted_members() -> Vec<AccountId> {
return vec![ALICE];
}
}
impl Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityOrigin = EnsureRoot<AccountId>;
Expand All @@ -148,6 +156,7 @@ impl Config for Test {
type MaxUniqueEntries = ConstU32<45>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
type BifrostOrigin = frame_system::EnsureSignedBy<BifrostAcc, AccountId>;
type WeightInfo = ();
}

Expand Down
6 changes: 6 additions & 0 deletions pallets/ema-oracle/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn add_oracle() -> Weight;
fn remove_oracle() -> Weight;
fn update_bifrost_oracle() -> Weight;
fn on_finalize_no_entry() -> Weight;
fn on_finalize_multiple_tokens(b: u32) -> Weight;
fn on_trade_multiple_tokens(b: u32) -> Weight;
Expand Down Expand Up @@ -87,6 +88,11 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(2_u64))
.saturating_add(RocksDbWeight::get().writes(5_u64))
}

fn update_bifrost_oracle() -> Weight {
Weight::zero()
}

/// Storage: `EmaOracle::Accumulator` (r:1 w:0)
/// Proof: `EmaOracle::Accumulator` (`max_values`: Some(1), `max_size`: Some(5921), added: 6416, mode: `MaxEncodedLen`)
fn on_finalize_no_entry() -> Weight {
Expand Down
2 changes: 1 addition & 1 deletion pallets/omnipool-liquidity-mining/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-omnipool-liquidity-mining"
version = "2.6.1"
version = "2.6.2"
authors = ['GalacticCouncil']
edition = "2021"
license = "Apache-2.0"
Expand Down
10 changes: 9 additions & 1 deletion pallets/omnipool-liquidity-mining/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use frame_support::BoundedVec;
use hydradx_traits::liquidity_mining::PriceAdjustment;
use pallet_omnipool;

use frame_support::traits::{ConstU128, Contains, Everything};
use frame_support::traits::{ConstU128, Contains, Everything, SortedMembers};
use frame_support::{
assert_ok, construct_runtime, parameter_types,
traits::{ConstU32, ConstU64},
Expand Down Expand Up @@ -273,13 +273,21 @@ parameter_types! {
OraclePeriod::LastBlock, OraclePeriod::Short, OraclePeriod::TenMinutes]);
}

pub struct BifrostAcc;
impl SortedMembers<AccountId> for BifrostAcc {
fn sorted_members() -> Vec<AccountId> {
return vec![ALICE];
}
}

impl pallet_ema_oracle::Config for Test {
type RuntimeEvent = RuntimeEvent;
type AuthorityOrigin = EnsureRoot<AccountId>;
type BlockNumberProvider = MockBlockNumberProvider;
type SupportedPeriods = SupportedPeriods;
type OracleWhitelist = Everything;
type MaxUniqueEntries = ConstU32<20>;
type BifrostOrigin = frame_system::EnsureSignedBy<BifrostAcc, AccountId>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
type WeightInfo = ();
Expand Down
2 changes: 1 addition & 1 deletion pallets/omnipool/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-omnipool"
version = "4.5.2"
version = "4.5.3"
authors = ['GalacticCouncil']
edition = "2021"
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion runtime/hydradx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hydradx-runtime"
version = "285.0.0"
version = "286.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
license = "Apache 2.0"
Expand Down
11 changes: 10 additions & 1 deletion runtime/hydradx/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use frame_support::{
sp_runtime::{FixedU128, Perbill, Permill},
traits::{
AsEnsureOriginWithArg, ConstU32, Contains, Currency, Defensive, EitherOf, EnsureOrigin, Imbalance,
LockIdentifier, NeverEnsureOrigin, OnUnbalanced,
LockIdentifier, NeverEnsureOrigin, OnUnbalanced, SortedMembers,
},
BoundedVec, PalletId,
};
Expand Down Expand Up @@ -580,9 +580,18 @@ where
}
}

pub struct BifrostAcc;
impl SortedMembers<AccountId> for BifrostAcc {
fn sorted_members() -> Vec<AccountId> {
//13YMK2eeopZtUNpeHnJ1Ws2HqMQG6Ts9PGCZYGyFbSYoZfcm
return vec![hex!["70617261ee070000000000000000000000000000000000000000000000000000"].into()];
}
}

impl pallet_ema_oracle::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type AuthorityOrigin = EitherOf<EnsureRoot<Self::AccountId>, GeneralAdmin>;
type BifrostOrigin = frame_system::EnsureSignedBy<BifrostAcc, AccountId>;
/// The definition of the oracle time periods currently assumes a 6 second block time.
/// We use the parachain blocks anyway, because we want certain guarantees over how many blocks correspond
/// to which smoothing factor.
Expand Down
2 changes: 1 addition & 1 deletion runtime/hydradx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("hydradx"),
impl_name: create_runtime_str!("hydradx"),
authoring_version: 1,
spec_version: 285,
spec_version: 286,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
12 changes: 11 additions & 1 deletion runtime/hydradx/src/weights/pallet_ema_oracle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,14 @@ impl<T: frame_system::Config> pallet_ema_oracle::WeightInfo for HydraWeight<T> {
Weight::from_parts(17_680_000, 6294)
.saturating_add(T::DbWeight::get().reads(2_u64))
}
}

//TODO: tmp, write propper benchmark
fn update_bifrost_oracle() -> Weight {
// Proof Size summary in bytes:
// Measured: `638`
// Estimated: `6294`
// Minimum execution time: 17_336_000 picoseconds.
Weight::from_parts(17_680_000, 6294)
.saturating_add(T::DbWeight::get().reads(2_u64))
}
}

0 comments on commit 6228871

Please sign in to comment.