Skip to content

Commit

Permalink
Runtime API
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard committed Dec 5, 2023
1 parent 7edfa03 commit d72e970
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 5 deletions.
10 changes: 10 additions & 0 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ pallet-inflation = { path = "./pallets/inflation", default-features = false }
pallet-dynamic-evm-base-fee = { path = "./pallets/dynamic-evm-base-fee", default-features = false }
pallet-unified-accounts = { path = "./pallets/unified-accounts", default-features = false }

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

astar-primitives = { path = "./primitives", default-features = false }
astar-test-utils = { path = "./tests/utils", default-features = false }

Expand Down
25 changes: 25 additions & 0 deletions pallets/dapp-staking-v3/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "dapp-staking-v3-runtime-api"
version = "0.0.1-alpha"
description = "Pallet for dApp staking v3 protocol"
authors.workspace = true
edition.workspace = true
homepage.workspace = true
repository.workspace = true

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-api = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.43", default-features = false }

astar-primitives = { workspace = true }
pallet-dapp-staking-v3 = { workspace = true }

[features]
default = ["std"]
std = [
"sp-api/std",
"pallet-dapp-staking-v3/std",
"astar-primitives/std",
]
40 changes: 40 additions & 0 deletions pallets/dapp-staking-v3/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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_attr(not(feature = "std"), no_std)]

use astar_primitives::BlockNumber;
use pallet_dapp_staking_v3::EraNumber;

sp_api::decl_runtime_apis! {

/// DApp Staking Api.
///
/// Used to provide information otherwise not available via RPC.
pub trait DAppStakingApi {

/// For how many standard era lengths does the voting subperiod last.
fn eras_per_voting_subperiod() -> EraNumber;

/// How many standard eras are there in the build&earn subperiod.
fn eras_per_build_and_earn_subperiod() -> EraNumber;

/// How many blocks are there per standard era.
fn blocks_per_era() -> BlockNumber;
}
}
3 changes: 1 addition & 2 deletions pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ mod test;
mod benchmarking;

mod types;
use types::*;
pub use types::{PriceProvider, TierThreshold};
pub use types::*;

pub mod weights;
pub use weights::WeightInfo;
Expand Down
3 changes: 3 additions & 0 deletions runtime/local/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ pallet-inflation = { workspace = true }
pallet-unified-accounts = { workspace = true }
pallet-xvm = { workspace = true }

dapp-staking-v3-runtime-api = { workspace = true }

# Moonbeam tracing
moonbeam-evm-tracer = { workspace = true, optional = true }
moonbeam-rpc-primitives-debug = { workspace = true, optional = true }
Expand Down Expand Up @@ -122,6 +124,7 @@ std = [
"pallet-chain-extension-unified-accounts/std",
"pallet-dapps-staking/std",
"pallet-dapp-staking-v3/std",
"dapp-staking-v3-runtime-api/std",
"pallet-inflation/std",
"pallet-dynamic-evm-base-fee/std",
"pallet-ethereum/std",
Expand Down
20 changes: 17 additions & 3 deletions runtime/local/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
use frame_support::{
construct_runtime, parameter_types,
traits::{
fungible::Unbalanced as FunUnbalanced, AsEnsureOriginWithArg, ConstU128, ConstU32,
ConstU64, Currency, EitherOfDiverse, EqualPrivilegeOnly, FindAuthor, Get, InstanceFilter,
Nothing, OnFinalize, WithdrawReasons,
AsEnsureOriginWithArg, ConstU128, ConstU32, ConstU64, Currency, EitherOfDiverse,
EqualPrivilegeOnly, FindAuthor, Get, InstanceFilter, Nothing, OnFinalize, WithdrawReasons,
},
weights::{
constants::{ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_REF_TIME_PER_SECOND},
Expand Down Expand Up @@ -523,6 +522,7 @@ impl pallet_dapp_staking_v3::BenchmarkHelper<SmartContract<AccountId>, AccountId
}

fn set_balance(account: &AccountId, amount: Balance) {
use frame_support::traits::fungible::Unbalanced as FunUnbalanced;
Balances::write_balance(account, amount)
.expect("Must succeed in test/benchmark environment.");
}
Expand Down Expand Up @@ -1739,6 +1739,20 @@ impl_runtime_apis! {
}
}

impl dapp_staking_v3_runtime_api::DAppStakingApi<Block> for Runtime {
fn eras_per_voting_subperiod() -> pallet_dapp_staking_v3::EraNumber {
InflationCycleConfig::eras_per_voting_subperiod()
}

fn eras_per_build_and_earn_subperiod() -> pallet_dapp_staking_v3::EraNumber {
InflationCycleConfig::eras_per_build_and_earn_subperiod()
}

fn blocks_per_era() -> BlockNumber {
InflationCycleConfig::blocks_per_era()
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn benchmark_metadata(extra: bool) -> (
Expand Down

0 comments on commit d72e970

Please sign in to comment.