Skip to content

Commit

Permalink
dapp staking v3 - Oracle solution (#1197)
Browse files Browse the repository at this point in the history
* Init commit

* Progress on the implementation

* More functionality

* Price type updates, docs, functionality implementation

* Tests, minor updates

* More test & some fixes

* Tests & minor fixes

* Finished implementation

* Docs

* Year change to make license check happy

* Full integration - init version

* genesis

* Move dummy combiner to primitives

* Changes

* orml oracle benchmarks dummy pallet

* Temporary benchmarks

* Comment

* Resolve TODOs

* Price aggregator weights

* Post merge adjustments

* Weights update

* Integration tests

* Remove comment

* Fix UT

* dApp staking v3 update

* Comments
  • Loading branch information
Dinonard authored Apr 2, 2024
1 parent b0181fb commit 4a4165c
Show file tree
Hide file tree
Showing 29 changed files with 2,162 additions and 57 deletions.
67 changes: 67 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pallet-scheduler = { git = "https://github.com/paritytech/polkadot-sdk", branch
pallet-treasury = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }
pallet-grandpa = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }
pallet-message-queue = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }
pallet-membership = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.1.0", default-features = false }

# EVM & Ethereum
# (wasm)
Expand Down Expand Up @@ -266,6 +267,7 @@ polkadot-service = { git = "https://github.com/paritytech/polkadot-sdk", branch
orml-xtokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v1.1.0", default-features = false }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v1.1.0", default-features = false }
orml-xcm-support = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v1.1.0", default-features = false }
orml-oracle = { git = "https://github.com/open-web3-stack/open-runtime-module-library", branch = "polkadot-v1.1.0", default-features = false }

# Astar pallets & modules
# (wasm)
Expand All @@ -282,6 +284,10 @@ pallet-dynamic-evm-base-fee = { path = "./pallets/dynamic-evm-base-fee", default
pallet-unified-accounts = { path = "./pallets/unified-accounts", default-features = false }
astar-xcm-benchmarks = { path = "./pallets/astar-xcm-benchmarks", default-features = false }
pallet-static-price-provider = { path = "./pallets/static-price-provider", default-features = false }
pallet-price-aggregator = { path = "./pallets/price-aggregator", default-features = false }

# Get rid of this once orml-oracle has been updated with benchmarks
oracle-benchmarks = { path = "./pallets/oracle-benchmarks", default-features = false }

dapp-staking-v3-runtime-api = { path = "./pallets/dapp-staking-v3/rpc/runtime-api", default-features = false }

Expand Down
22 changes: 19 additions & 3 deletions bin/collator/src/parachain/chain_spec/shibuya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ use sc_service::ChainType;
use shibuya_runtime::{
wasm_binary_unwrap, AccountId, AuraConfig, AuraId, Balance, BalancesConfig,
CollatorSelectionConfig, CouncilConfig, DappStakingConfig, DemocracyConfig, EVMChainIdConfig,
EVMConfig, InflationConfig, InflationParameters, ParachainInfoConfig, Precompiles,
RuntimeGenesisConfig, SessionConfig, SessionKeys, Signature, SudoConfig, SystemConfig,
TechnicalCommitteeConfig, TierThreshold, TreasuryConfig, VestingConfig, SBY,
EVMConfig, InflationConfig, InflationParameters, OracleMembershipConfig, ParachainInfoConfig,
Precompiles, PriceAggregatorConfig, RuntimeGenesisConfig, SessionConfig, SessionKeys,
Signature, SudoConfig, SystemConfig, TechnicalCommitteeConfig, TierThreshold, TreasuryConfig,
VestingConfig, SBY,
};
use sp_core::{sr25519, Pair, Public};

use astar_primitives::oracle::CurrencyAmount;
use sp_runtime::{
traits::{IdentifyAccount, Verify},
Permill,
Expand Down Expand Up @@ -206,6 +208,20 @@ fn make_genesis(
params: InflationParameters::default(),
..Default::default()
},
oracle_membership: OracleMembershipConfig {
members: vec![
get_account_id_from_seed::<sr25519::Public>("Alice"),
get_account_id_from_seed::<sr25519::Public>("Bob"),
]
.try_into()
.expect("Assumption is that at least two members will be allowed."),
..Default::default()
},
price_aggregator: PriceAggregatorConfig {
circular_buffer: vec![CurrencyAmount::from_rational(5, 10)]
.try_into()
.expect("Must work since buffer should have at least a single value."),
},
}
}

Expand Down
6 changes: 3 additions & 3 deletions pallets/dapp-staking-v3/src/test/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use frame_support::{
traits::{fungible::Mutate as FunMutate, ConstU128, ConstU32},
weights::Weight,
};
use sp_arithmetic::fixed_point::FixedU64;
use sp_arithmetic::fixed_point::FixedU128;
use sp_core::H256;
use sp_io::TestExternalities;
use sp_runtime::{
Expand Down Expand Up @@ -106,8 +106,8 @@ impl pallet_balances::Config for Test {

pub struct DummyPriceProvider;
impl PriceProvider for DummyPriceProvider {
fn average_price() -> FixedU64 {
FixedU64::from_rational(1, 10)
fn average_price() -> FixedU128 {
FixedU128::from_rational(1, 10)
}
}

Expand Down
6 changes: 3 additions & 3 deletions pallets/dapp-staking-v3/src/test/tests_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use astar_primitives::{dapp_staking::StandardTierSlots, Balance};
use frame_support::assert_ok;
use sp_arithmetic::fixed_point::FixedU64;
use sp_arithmetic::fixed_point::FixedU128;
use sp_runtime::Permill;

use crate::*;
Expand Down Expand Up @@ -2887,11 +2887,11 @@ fn tier_configuration_basic_tests() {
assert!(init_config.is_valid(), "Init config must be valid!");

// Create a new config, based on a new price
let high_price = FixedU64::from_rational(20, 100); // in production will be expressed in USD
let high_price = FixedU128::from_rational(20, 100); // in production will be expressed in USD
let new_config = init_config.calculate_new(high_price, &params);
assert!(new_config.is_valid());

let low_price = FixedU64::from_rational(1, 100); // in production will be expressed in USD
let low_price = FixedU128::from_rational(1, 100); // in production will be expressed in USD
let new_config = init_config.calculate_new(low_price, &params);
assert!(new_config.is_valid());

Expand Down
8 changes: 4 additions & 4 deletions pallets/dapp-staking-v3/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
use frame_support::{pallet_prelude::*, BoundedBTreeMap, BoundedVec};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use sp_arithmetic::fixed_point::FixedU64;
use sp_arithmetic::fixed_point::FixedU128;
use sp_runtime::{
traits::{CheckedAdd, UniqueSaturatedInto, Zero},
FixedPointNumber, Permill, Saturating,
Expand Down Expand Up @@ -1629,7 +1629,7 @@ impl<NT: Get<u32>, T: TierSlotsFunc> TiersConfiguration<NT, T> {
}

/// Calculate new `TiersConfiguration`, based on the old settings, current native currency price and tier configuration.
pub fn calculate_new(&self, native_price: FixedU64, params: &TierParameters<NT>) -> Self {
pub fn calculate_new(&self, native_price: FixedU128, params: &TierParameters<NT>) -> Self {
// It must always be at least 1 slot.
let new_number_of_slots = T::number_of_slots(native_price).max(1);

Expand Down Expand Up @@ -1671,7 +1671,7 @@ impl<NT: Get<u32>, T: TierSlotsFunc> TiersConfiguration<NT, T> {
// new_threshold = old_threshold * (1 + %_threshold)
//
let new_tier_thresholds = if new_number_of_slots > self.number_of_slots {
let delta_threshold_decrease = FixedU64::from_rational(
let delta_threshold_decrease = FixedU128::from_rational(
(new_number_of_slots - self.number_of_slots).into(),
new_number_of_slots.into(),
);
Expand All @@ -1693,7 +1693,7 @@ impl<NT: Get<u32>, T: TierSlotsFunc> TiersConfiguration<NT, T> {

new_tier_thresholds
} else if new_number_of_slots < self.number_of_slots {
let delta_threshold_increase = FixedU64::from_rational(
let delta_threshold_increase = FixedU128::from_rational(
(self.number_of_slots - new_number_of_slots).into(),
new_number_of_slots.into(),
);
Expand Down
52 changes: 52 additions & 0 deletions pallets/oracle-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[package]
name = "oracle-benchmarks"
version = "0.1.0"
license = "GPL-3.0-or-later"
description = "Temporary pallet to benchmark orml-oracle. Should be handled by orml-oracle itself in the future."
authors.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
log = { workspace = true }
parity-scale-codec = { workspace = true }
serde = { workspace = true }

frame-support = { workspace = true }
frame-system = { workspace = true }
scale-info = { workspace = true }
sp-arithmetic = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }

orml-oracle = { workspace = true }
orml-traits = { workspace = true }

astar-primitives = { workspace = true }

frame-benchmarking = { workspace = true, optional = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"log/std",
"scale-info/std",
"serde/std",
"sp-std/std",
"frame-support/std",
"frame-system/std",
"astar-primitives/std",
"sp-arithmetic/std",
"orml-traits/std",
"orml-oracle/std",
]
runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"astar-primitives/runtime-benchmarks",
]
try-runtime = ["frame-support/try-runtime"]
69 changes: 69 additions & 0 deletions pallets/oracle-benchmarks/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v2::*;
use frame_support::{
assert_ok,
traits::{Get, OnFinalize},
BoundedVec,
};
use frame_system::RawOrigin;
use sp_std::vec;

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn feed_values(x: Linear<1, { <T as orml_oracle::Config>::MaxFeedValues::get() }>) {
// Prepare account and add it as member
let account: T::AccountId = whitelisted_caller();
T::AddMember::add_member(account.clone());

// Get base feed value
let currency_id_price_pair = T::BenchmarkCurrencyIdValuePair::get();

// Prepare feed values vector
let mut key_value_pairs =
BoundedVec::<_, <T as orml_oracle::Config>::MaxFeedValues>::default();
for _ in 0..x {
key_value_pairs
.try_push(currency_id_price_pair.clone())
.unwrap();
}

#[block]
{
assert_ok!(orml_oracle::Pallet::<T>::feed_values(
RawOrigin::Signed(account.clone()).into(),
key_value_pairs
));
}
}

#[benchmark]
fn on_finalize() {
#[block]
{
orml_oracle::Pallet::<T>::on_finalize(1_u32.into());
}
}
}
Loading

0 comments on commit 4a4165c

Please sign in to comment.