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

Broker: sale price runtime api #3485

Merged
merged 19 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

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

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pallet-authorship = { path = "../../../../../substrate/frame/authorship", defaul
pallet-balances = { path = "../../../../../substrate/frame/balances", default-features = false }
pallet-message-queue = { path = "../../../../../substrate/frame/message-queue", default-features = false }
pallet-broker = { path = "../../../../../substrate/frame/broker", default-features = false }
pallet-broker-runtime-api = { path = "../../../../../substrate/frame/broker/runtime-api", default-features = false }
pallet-multisig = { path = "../../../../../substrate/frame/multisig", default-features = false }
pallet-session = { path = "../../../../../substrate/frame/session", default-features = false }
pallet-sudo = { path = "../../../../../substrate/frame/sudo", default-features = false }
Expand Down Expand Up @@ -101,6 +102,7 @@ std = [
"pallet-authorship/std",
"pallet-balances/std",
"pallet-broker/std",
"pallet-broker-runtime-api/std",
"pallet-collator-selection/std",
"pallet-message-queue/std",
"pallet-multisig/std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,12 @@ impl_runtime_apis! {
}
}

impl pallet_broker_runtime_api::BrokerApi<Block, Balance> for Runtime {
fn sale_price() -> Option<Balance> {
Copy link
Member

Choose a reason for hiding this comment

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

And do we maybe want to expose more stuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be useful to fetch the unclaimed revenue of a region contributed to the instantaneous pool. I can add that as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be useful to fetch the unclaimed revenue of a region contributed to the instantaneous pool.

After thinking about this a bit more not sure if adding this would make sense. Getting the unclaimed revenue can require quite a lot of reading(reading InstaPoolHistory per each timeslice). So having this exposed could be an easy spam vector.

Broker::api_sale_price()
}
}

impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {
fn query_info(
uxt: <Block as BlockT>::Extrinsic,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pallet-authorship = { path = "../../../../../substrate/frame/authorship", defaul
pallet-balances = { path = "../../../../../substrate/frame/balances", default-features = false }
pallet-message-queue = { path = "../../../../../substrate/frame/message-queue", default-features = false }
pallet-broker = { path = "../../../../../substrate/frame/broker", default-features = false }
pallet-broker-runtime-api = { path = "../../../../../substrate/frame/broker/runtime-api", default-features = false }
pallet-multisig = { path = "../../../../../substrate/frame/multisig", default-features = false }
pallet-session = { path = "../../../../../substrate/frame/session", default-features = false }
pallet-timestamp = { path = "../../../../../substrate/frame/timestamp", default-features = false }
Expand Down Expand Up @@ -100,6 +101,7 @@ std = [
"pallet-authorship/std",
"pallet-balances/std",
"pallet-broker/std",
"pallet-broker-runtime-api/std",
"pallet-collator-selection/std",
"pallet-message-queue/std",
"pallet-multisig/std",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ impl_runtime_apis! {
}
}

impl pallet_broker_runtime_api::BrokerApi<Block, Balance> for Runtime {
fn sale_price() -> Option<Balance> {
Broker::api_sale_price()
}
}

impl pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance> for Runtime {
fn query_info(
uxt: <Block as BlockT>::Extrinsic,
Expand Down
26 changes: 26 additions & 0 deletions substrate/frame/broker/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "pallet-broker-runtime-api"
version = "14.0.0"
authors.workspace = true
edition.workspace = true
license = "Apache-2.0"
homepage = "https://substrate.io"
repository.workspace = true
description = "Runtime API for the FRAME Broker pallet."
readme = "README.md"

[lints]
workspace = true

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

[dependencies]
codec = { package = "parity-scale-codec", version = "3.6.1", default-features = false, features = ["derive"] }
pallet-broker = { path = "..", default-features = false }
sp-api = { path = "../../../primitives/api", default-features = false }
sp-std = { path = "../../../primitives/std", default-features = false }

[features]
default = ["std"]
std = ["codec/std", "pallet-broker/std", "sp-api/std", "sp-std/std"]
3 changes: 3 additions & 0 deletions substrate/frame/broker/runtime-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RPC runtime API for the FRAME Broker pallet.

License: Apache-2.0
32 changes: 32 additions & 0 deletions substrate/frame/broker/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// 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.

//! Runtime API definition for the FRAME Broker pallet.

#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;

sp_api::decl_runtime_apis! {
pub trait BrokerApi<Balance>
where
Balance: Codec
{
/// If there is an ongoing sale returns the current price of a core.
fn sale_price() -> Option<Balance>;
}
}
17 changes: 17 additions & 0 deletions substrate/frame/broker/src/dispatchable_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,21 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::AllowedRenewalDropped { core, when });
Ok(())
}

/// If there is a sale ongoing returns the current price of a core.
pub fn api_sale_price() -> Option<BalanceOf<T>> {
let Some(status) = Status::<T>::get() else {
return None;
};
let Some(sale) = SaleInfo::<T>::get() else {
return None;
};

if sale.first_core < status.core_count || sale.cores_sold < sale.cores_offered {
return None;
}
let now = frame_system::Pallet::<T>::block_number();
let price = Self::sale_price(&sale, now);
Some(price)
}
}
Loading