-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
|
||
}); | ||
|
||
} |