Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate EMA Oracle #599

Merged
merged 28 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
076e793
add EmaOracle to Basilisk runtime
apopiak Feb 7, 2023
c889078
update ema oracle to new liquidity type
apopiak Feb 21, 2023
3b4fbf4
Merge branch 'master' of github.com:galacticcouncil/Basilisk-node int…
apopiak Feb 21, 2023
93f6ea5
bump runtime version
apopiak Feb 21, 2023
e8d2dba
formatting
apopiak Feb 21, 2023
23a5532
update oracle pallet to most recent version
apopiak Feb 23, 2023
fc11ff3
bump crate versions
apopiak Feb 23, 2023
7e376bf
bump xyk LM benchmarking pallet version
apopiak Feb 23, 2023
af9bf56
reduce max unique oracle entries and adjust oracle periods
apopiak Feb 27, 2023
411c17d
update warehouse dep
apopiak Feb 27, 2023
09595b9
fix xyk LM compile errors in mocks
apopiak Feb 27, 2023
9dac5e4
adjust integration test to expect hourly oracle instead of ten minutes
apopiak Feb 27, 2023
785ef64
update ema-oracle to fix benchmarking issue
apopiak Mar 9, 2023
95c4a5b
use ema-oracle pallet version with fixed get_entry benchmark
apopiak Mar 9, 2023
10c6b51
add oracle weights file and use the contained weights
apopiak Mar 10, 2023
30411fb
Merge branch 'master' of github.com:galacticcouncil/Basilisk-node int…
apopiak Mar 10, 2023
54d2613
bump crate and spec versions
apopiak Mar 10, 2023
628697f
bump xyk LM benchmarking crate
apopiak Mar 10, 2023
fae5cf3
update warehouse deps to new oracle impl and new AMMTransfer trait
apopiak Apr 13, 2023
40d392d
run ci
mrq1911 Apr 18, 2023
93df908
Merge branch 'master' of github.com:galacticcouncil/Basilisk-node int…
apopiak May 3, 2023
6f533ba
bump crate versions
apopiak May 3, 2023
65d1327
Merge remote-tracking branch 'origin' into apopiak/oracle-integration
apopiak May 30, 2023
c9f308f
Merge branch 'master' of github.com:galacticcouncil/Basilisk-node int…
apopiak May 30, 2023
645c281
adjust integration test
apopiak May 30, 2023
781e60c
bump crate versions
apopiak May 30, 2023
b5c5c68
Merge branch 'master' into apopiak/oracle-integration
apopiak May 30, 2023
d72a5d2
Merge branch 'master' into apopiak/oracle-integration
mrq1911 May 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "0.9.4"
version = "0.9.5"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
1 change: 1 addition & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod kusama_test_net;
mod nft;
mod nft_marketplace;
mod non_native_fee;
mod oracle;
mod router;
mod vesting;
mod xyk;
74 changes: 74 additions & 0 deletions integration-tests/src/oracle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#![cfg(test)]

use crate::kusama_test_net::*;

use basilisk_runtime::{EmaOracle, RuntimeOrigin, XYK};
use frame_support::{
assert_ok,
traits::{OnFinalize, OnInitialize},
};
use hydradx_traits::{AggregatedPriceOracle, OraclePeriod::*};
use pallet_ema_oracle::OracleError;
use polkadot_primitives::v2::BlockNumber;
use xcm_emulator::TestExt;

pub fn basilisk_run_to_block(to: BlockNumber) {
while basilisk_runtime::System::block_number() < to {
let b = basilisk_runtime::System::block_number();

basilisk_runtime::System::on_finalize(b);
basilisk_runtime::EmaOracle::on_finalize(b);

basilisk_runtime::System::on_initialize(b + 1);
basilisk_runtime::EmaOracle::on_initialize(b + 1);

basilisk_runtime::System::set_block_number(b + 1);
}
}

use pallet_xyk::SOURCE;

#[test]
fn xyk_trades_are_ingested_into_oracle() {
TestNet::reset();

let asset_a = 1;
let asset_b = 2;

Basilisk::execute_with(|| {
// arrange
basilisk_run_to_block(2);

assert_ok!(XYK::create_pool(
RuntimeOrigin::signed(ALICE.into()),
asset_a,
100 * UNITS,
asset_b,
200 * UNITS,
));
assert_ok!(XYK::sell(
RuntimeOrigin::signed(ALICE.into()),
asset_a,
asset_b,
5 * UNITS,
UNITS,
false,
));

// act
// will store the data received in the sell as oracle values
basilisk_run_to_block(3);

// assert
let expected = ((105000000000000, 190504761904760).into(), 0);
assert_eq!(EmaOracle::get_price(asset_a, asset_b, LastBlock, SOURCE), Ok(expected));
// ten minutes oracle not configured/supported
assert_eq!(
EmaOracle::get_price(asset_a, asset_b, TenMinutes, SOURCE),
Err(OracleError::NotPresent)
);
assert_eq!(EmaOracle::get_price(asset_a, asset_b, Hour, SOURCE), Ok(expected));
assert_eq!(EmaOracle::get_price(asset_a, asset_b, Day, SOURCE), Ok(expected));
assert_eq!(EmaOracle::get_price(asset_a, asset_b, Week, SOURCE), Ok(expected));
});
}
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basilisk"
version = "9.0.0"
version = "9.0.1"
description = "Basilisk node"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
2 changes: 2 additions & 0 deletions node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,7 @@ fn parachain_genesis(
dust_account: Some(hex!["6d6f646c70792f74727372790000000000000000000000000000000000000000"].into()),
},
polkadot_xcm: Default::default(),
ema_oracle: Default::default(),
xyk_liquidity_mining: Default::default(),
xyk_warehouse_lm: Default::default(),
}
Expand Down Expand Up @@ -830,6 +831,7 @@ fn testnet_parachain_genesis(
dust_account: Some(get_account_id_from_seed::<sr25519::Public>("Duster")),
},
polkadot_xcm: Default::default(),
ema_oracle: Default::default(),
xyk_liquidity_mining: Default::default(),
xyk_warehouse_lm: Default::default(),
}
Expand Down
1 change: 1 addition & 0 deletions node/src/testing_chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,6 @@ fn testnet_parachain_genesis(
polkadot_xcm: Default::default(),
xyk_liquidity_mining: Default::default(),
xyk_warehouse_lm: Default::default(),
ema_oracle: Default::default(),
}
}
2 changes: 1 addition & 1 deletion runtime/basilisk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "basilisk-runtime"
version = "96.0.0"
version = "97.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
homepage = "https://github.com/galacticcouncil/Basilisk-node"
Expand Down
31 changes: 28 additions & 3 deletions runtime/basilisk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ use frame_support::{
constants::{BlockExecutionWeight, RocksDbWeight},
ConstantMultiplier, Weight, WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
},
BoundedVec,
};
use hydradx_traits::AssetPairAccountIdFor;
use hydradx_traits::{AssetPairAccountIdFor, OraclePeriod};
use pallet_transaction_payment::TargetedFeeAdjustment;
pub use sp_consensus_aura::sr25519::AuthorityId as AuraId;

Expand Down Expand Up @@ -112,7 +113,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("basilisk"),
impl_name: create_runtime_str!("basilisk"),
authoring_version: 1,
spec_version: 96,
spec_version: 97,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -494,7 +495,7 @@ impl pallet_xyk::Config for Runtime {
type MaxInRatio = MaxInRatio;
type MaxOutRatio = MaxOutRatio;
type CanCreatePool = pallet_lbp::DisallowWhenLBPPoolRunning<Runtime>;
type AMMHandler = ();
type AMMHandler = pallet_ema_oracle::OnActivityHandler<Runtime>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't be xyk reweighted now with oracle?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah

type DiscountedFee = DiscountedFee;
type NonDustableWhitelistHandler = Duster;
}
Expand Down Expand Up @@ -963,6 +964,26 @@ impl pallet_collator_rewards::Config for Runtime {
type SessionManager = CollatorSelection;
}

// constants need to be in scope to be used in generics
use pallet_ema_oracle::MAX_PERIODS;

parameter_types! {
pub SupportedPeriods: BoundedVec<OraclePeriod, ConstU32<MAX_PERIODS>> = BoundedVec::truncate_from(
vec![OraclePeriod::LastBlock, OraclePeriod::Hour, OraclePeriod::Day, OraclePeriod::Week]
);
// There are currently only a few pools, so the number of entries per block is limited.
// NOTE: Needs to be updated once the number of pools grows.
pub MaxUniqueOracleEntries: u32 = 30;
}

impl pallet_ema_oracle::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = weights::ema_oracle::BasiliskWeight<Runtime>;
type BlockNumberProvider = RelayChainBlockNumberProvider<Runtime>;
type SupportedPeriods = SupportedPeriods;
type MaxUniqueEntries = MaxUniqueOracleEntries;
}

// Create the runtime by composing the FRAME pallets that were previously configured.
construct_runtime!(
pub enum Runtime where
Expand Down Expand Up @@ -1024,6 +1045,8 @@ construct_runtime!(
XYKWarehouseLM: warehouse_liquidity_mining::<Instance1> = 113,
CollatorRewards: pallet_collator_rewards = 114,

EmaOracle: pallet_ema_oracle = 120,

// ORML related modules - runtime module index for orml starts at 150
Currencies: pallet_currencies = 150,
Tokens: orml_tokens = 151,
Expand Down Expand Up @@ -1235,6 +1258,7 @@ impl_runtime_apis! {
list_benchmark!(list, extra, pallet_asset_registry, AssetRegistry);
list_benchmark!(list, extra, pallet_xyk_liquidity_mining, XYKLiquidityMiningBench::<Runtime>);
list_benchmark!(list, extra, pallet_transaction_pause, TransactionPause);
list_benchmark!(list, extra, pallet_ema_oracle, EmaOracle);

list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>);
list_benchmark!(list, extra, pallet_balances, Balances);
Expand Down Expand Up @@ -1299,6 +1323,7 @@ impl_runtime_apis! {
add_benchmark!(params, batches, pallet_asset_registry, AssetRegistry);
add_benchmark!(params, batches, pallet_xyk_liquidity_mining, XYKLiquidityMiningBench::<Runtime>);
add_benchmark!(params, batches, pallet_transaction_pause, TransactionPause);
add_benchmark!(params, batches, pallet_ema_oracle, EmaOracle);

// Substrate pallets
add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
Expand Down
3 changes: 2 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "common-runtime"
version = "2.3.10"
version = "2.3.11"
authors = ["GalacticCouncil"]
edition = "2021"
homepage = "https://github.com/galacticcouncil/Basilisk-node"
Expand Down Expand Up @@ -33,6 +33,7 @@ pallet-transaction-pause = { git = "https://github.com/galacticcouncil/warehouse
pallet-currencies = { git = "https://github.com/galacticcouncil/warehouse", rev = "d6a78b5d51bc8af525d2b8f856efcfce2159e334", default-features = false }
pallet-route-executor = { git = "https://github.com/galacticcouncil/warehouse", rev = "d6a78b5d51bc8af525d2b8f856efcfce2159e334", default-features = false }
pallet-duster = { git = "https://github.com/galacticcouncil/warehouse", rev = "d6a78b5d51bc8af525d2b8f856efcfce2159e334", default-features = false }
pallet-ema-oracle = { git = "https://github.com/galacticcouncil/warehouse", rev = "d6a78b5d51bc8af525d2b8f856efcfce2159e334", default-features = false }

# Substrate dependencies
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38", default-features = false }
Expand Down
79 changes: 79 additions & 0 deletions runtime/common/src/weights/ema_oracle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// This file is part of Basilisk-node.

// Copyright (C) 2020-2023 Intergalactic, Limited (GIB).
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Autogenerated weights for pallet_ema_oracle
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2023-03-09, STEPS: 10, REPEAT: 30, LOW RANGE: [], HIGH RANGE: []
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
// target/release/basilisk
// benchmark
// pallet
// --chain=dev
// --steps=10
// --repeat=30
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --template=.maintain/pallet-weight-template-no-back.hbs
// --pallet=pallet_ema_oracle
// --output=oracle.rs
// --extrinsic=*
#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(clippy::unnecessary_cast)]

use frame_support::{
traits::Get,
weights::{constants::RocksDbWeight, Weight},
};
use sp_std::marker::PhantomData;

use pallet_ema_oracle::weights::WeightInfo;

pub struct BasiliskWeight<T>(PhantomData<T>);

impl<T: frame_system::Config> WeightInfo for BasiliskWeight<T> {
fn on_finalize_no_entry() -> Weight {
Weight::from_ref_time(4_740_000).saturating_add(T::DbWeight::get().reads(1 as u64))
}
fn on_finalize_multiple_tokens(b: u32) -> Weight {
Weight::from_ref_time(19_041_000) // Standard Error: 53_000
.saturating_add(Weight::from_ref_time(43_566_000).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().reads((4 as u64).saturating_mul(b as u64)))
.saturating_add(T::DbWeight::get().writes(1 as u64))
.saturating_add(T::DbWeight::get().writes((4 as u64).saturating_mul(b as u64)))
}
fn on_trade_multiple_tokens(b: u32) -> Weight {
Weight::from_ref_time(17_201_000) // Standard Error: 6_000
.saturating_add(Weight::from_ref_time(712_000).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
fn on_liquidity_changed_multiple_tokens(b: u32) -> Weight {
Weight::from_ref_time(17_238_000) // Standard Error: 6_000
.saturating_add(Weight::from_ref_time(704_000).saturating_mul(b as u64))
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
fn get_entry() -> Weight {
Weight::from_ref_time(24_093_000).saturating_add(T::DbWeight::get().reads(2 as u64))
}
}
1 change: 1 addition & 0 deletions runtime/common/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod collator_selection;
pub mod currencies;
pub mod democracy;
pub mod duster;
pub mod ema_oracle;
pub mod lbp;
pub mod marketplace;
pub mod nft;
Expand Down
2 changes: 1 addition & 1 deletion runtime/testing-basilisk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "testing-basilisk-runtime"
version = "96.0.0"
version = "97.0.0"
authors = ["GalacticCouncil"]
edition = "2021"
homepage = "https://github.com/galacticcouncil/Basilisk-node"
Expand Down