Skip to content

Commit

Permalink
Runtime APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jul 9, 2024
1 parent 7d2349a commit 1e5d5ad
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pallets/funding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pallet-xcm.workspace = true
polkadot-parachain-primitives.workspace = true
polimec-common-test-utils = { workspace = true, optional = true }
frame-benchmarking = { workspace = true, optional = true }
sp-api.workspace = true

# Used in the instantiator.
itertools.workspace = true
Expand Down
1 change: 1 addition & 0 deletions pallets/funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub mod instantiator;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
mod runtime_api;

pub type AccountIdOf<T> = <T as frame_system::Config>::AccountId;
pub type ProjectId = u32;
Expand Down
22 changes: 21 additions & 1 deletion pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use super::*;
use crate as pallet_funding;
use crate::traits::ProvideAssetPrice;
use crate::{runtime_api::Leaderboards, traits::ProvideAssetPrice};
use frame_support::{
construct_runtime,
pallet_prelude::Weight,
Expand Down Expand Up @@ -522,3 +522,23 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
});
ext
}

sp_api::mock_impl_runtime_apis! {
impl Leaderboards<Block, TestRuntime> for TestRuntime {
fn top_evaluators(project_id: ProjectId, amount: u32) -> Vec<EvaluationInfoOf<TestRuntime>> {
PolimecFunding::top_evaluators(project_id, amount)
}

fn top_bidders(project_id: ProjectId, amount: u32) -> Vec<BidInfoOf<TestRuntime>> {
vec![]
}

fn top_contributors(project_id: ProjectId, amount: u32) -> Vec<ContributionInfoOf<TestRuntime>> {
vec![]
}

fn top_projects(amount: u32) -> Vec<(ProjectId, ProjectMetadataOf<TestRuntime>, ProjectDetailsOf<TestRuntime>)> {
vec![]
}
}
}
37 changes: 37 additions & 0 deletions pallets/funding/src/runtime_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::*;
use itertools::Itertools;

sp_api::decl_runtime_apis! {
#[api_version(1)]
pub trait Leaderboards<T: Config> {
fn top_evaluators(project_id: ProjectId, amount: u32) -> Vec<EvaluationInfoOf<T>>;

fn top_bidders(project_id: ProjectId, amount: u32) -> Vec<BidInfoOf<T>>;

fn top_contributors(project_id: ProjectId, amount: u32) -> Vec<ContributionInfoOf<T>>;

fn top_projects(amount: u32) -> Vec<(ProjectId, ProjectMetadataOf<T>, ProjectDetailsOf<T>)>;
}

#[api_version(1)]
pub trait UserInformation<T: Config> {
/// Get all the contribution token balances for the participated projects
fn contribution_tokens(account: AccountIdOf<T>) -> Option<Vec<(ProjectId, BalanceOf<T>)>>;
}

#[api_version(1)]
pub trait ExtrinsicHelpers<T: Config> {
/// Get the current price of a contribution token (either current bucket in the auction, or WAP in contribution phase),
/// and calculate the amount of tokens that can be bought with the given amount USDT/USDC/DOT.
fn funding_asset_to_ct_amount(asset: AcceptedFundingAsset, asset_amount: BalanceOf<T>) -> Result<BalanceOf<T>, ()>;
}
}

impl<T: Config> Pallet<T> {
pub fn top_evaluators(project_id: ProjectId, amount: u32) -> Vec<EvaluationInfoOf<T>> {
Evaluations::<T>::iter_prefix_values((project_id,))
.sorted_by(|a, b| b.original_plmc_bond.cmp(&a.original_plmc_bond))
.take(amount as usize)
.collect_vec()
}
}
1 change: 1 addition & 0 deletions pallets/funding/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ mod funding_end;
mod misc;
#[path = "5_remainder.rs"]
mod remainder;
mod runtime_api;
#[path = "7_settlement.rs"]
mod settlement;

Expand Down
32 changes: 32 additions & 0 deletions pallets/funding/src/tests/runtime_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::*;
use crate::runtime_api::Leaderboards;

#[test]
fn top_evaluators() {
let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext())));
let evaluations = vec![
UserToUSDBalance::new(EVALUATOR_1, 500_000 * USD_UNIT),
UserToUSDBalance::new(EVALUATOR_2, 250_000 * USD_UNIT),
UserToUSDBalance::new(EVALUATOR_3, 320_000 * USD_UNIT),
UserToUSDBalance::new(EVALUATOR_4, 1_000_000 * USD_UNIT),
UserToUSDBalance::new(EVALUATOR_1, 1_000 * USD_UNIT),
];
let project_id =
inst.create_auctioning_project(default_project_metadata(ISSUER_1), ISSUER_1, evaluations);

inst.execute(|| {
let block_hash = System::block_hash(System::block_number());
let top_1 = TestRuntime::top_evaluators(&TestRuntime, block_hash, project_id, 1).unwrap();
let evaluator_4_evaluation = Evaluations::<TestRuntime>::get((project_id, EVALUATOR_4, 3)).unwrap();
assert!(top_1.len() == 1 && top_1[0] == evaluator_4_evaluation);

let top_4_evaluators = TestRuntime::top_evaluators(&TestRuntime, block_hash, project_id, 4).unwrap().into_iter().map(|evaluation|evaluation.evaluator).collect_vec();
assert_eq!(top_4_evaluators, vec![EVALUATOR_4, EVALUATOR_1, EVALUATOR_3, EVALUATOR_2]);

// cannot get more than the number of evaluations
let top_6_evaluators = TestRuntime::top_evaluators(&TestRuntime, block_hash, project_id, 6).unwrap().into_iter().map(|evaluation|evaluation.evaluator).collect_vec();
assert_eq!(top_6_evaluators, vec![EVALUATOR_4, EVALUATOR_1, EVALUATOR_3, EVALUATOR_2, EVALUATOR_1]);

});

}

0 comments on commit 1e5d5ad

Please sign in to comment.