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

✨ Runtime API to retrieve evaluation/participation made by a user #463

Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ default:

# Build the "Base" Runtime using srtool
build-polimec-polkadot-srtool:
srtool build --root -p polimec-runtime --profile production --runtime-dir runtimes/polimec --build-opts="--features=on-chain-release-build" --no-wasm-std
srtool build --root -p polimec-runtime --profile production --runtime-dir runtimes/polimec --build-opts="--features=on-chain-release-build"

build-polimec-paseo-srtool:
srtool build --root -p polimec-runtime --profile production --runtime-dir runtimes/polimec --build-opts="--features=on-chain-release-build,fast-mode" --no-wasm-std
srtool build --root -p polimec-runtime --profile production --runtime-dir runtimes/polimec --build-opts="--features=on-chain-release-build,fast-mode"

# Test the runtimes features
test-runtime-features runtime="polimec-runtime":
Expand Down
34 changes: 32 additions & 2 deletions pallets/funding/src/functions/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ sp_api::decl_runtime_apis! {
fn top_projects_by_usd_target_percent_reached(amount: u32) -> Vec<(ProjectId, ProjectMetadataOf<T>, ProjectDetailsOf<T>)>;
}

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

/// Get all the `EvaluationInfoOf` made by a single account, for a specific project if provided.
fn evaluations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<EvaluationInfoOf<T>>;

/// Get all the `BidInfoOf` made by a single account, for a specific project if provided.
fn participations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<BidInfoOf<T>>;
}

#[api_version(1)]
Expand Down Expand Up @@ -82,6 +88,30 @@ impl<T: Config> Pallet<T> {
.collect_vec()
}

pub fn participations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<BidInfoOf<T>> {
match project_id {
Some(id) => Bids::<T>::iter_prefix_values(id).filter(|bid| bid.bidder == account).collect_vec(),
None => Bids::<T>::iter_values().filter(|bid| bid.bidder == account).collect_vec(),
}
}

pub fn evaluations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<EvaluationInfoOf<T>> {
match project_id {
Some(id) => {
// Use both project ID and account as prefix
let prefix = (id, account);
Evaluations::<T>::iter_prefix_values(prefix).collect_vec()
},
None => {
// If no project is specified, iterate over all projects for this account
Evaluations::<T>::iter()
.filter(|((_, evaluator, _), _)| *evaluator == account)
.map(|(_, value)| value)
.collect_vec()
},
}
}

pub fn top_projects_by_usd_raised(amount: u32) -> Vec<(ProjectId, ProjectMetadataOf<T>, ProjectDetailsOf<T>)> {
ProjectsDetails::<T>::iter()
.sorted_by(|a, b| b.1.funding_amount_reached_usd.cmp(&a.1.funding_amount_reached_usd))
Expand Down
9 changes: 5 additions & 4 deletions pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ parameter_types! {
pub const ApprovalDeposit: Balance = EXISTENTIAL_DEPOSIT;

}

impl pallet_assets::Config<ContributionTokensInstance> for TestRuntime {
type ApprovalDeposit = ApprovalDeposit;
type AssetAccountDeposit = ZeroAssetAccountDeposit;
Expand All @@ -180,12 +181,14 @@ impl pallet_assets::Config<ContributionTokensInstance> for TestRuntime {

#[cfg(feature = "runtime-benchmarks")]
pub struct PalletAssetsBenchmarkHelper;

#[cfg(feature = "runtime-benchmarks")]
impl pallet_assets::BenchmarkHelper<Location> for PalletAssetsBenchmarkHelper {
fn create_asset_id_parameter(id: u32) -> Location {
(Parent, Parachain(id)).into()
}
}

impl pallet_assets::Config<ForeignAssetsInstance> for TestRuntime {
type ApprovalDeposit = ApprovalDeposit;
type AssetAccountDeposit = AssetAccountDeposit;
Expand All @@ -208,6 +211,7 @@ impl pallet_assets::Config<ForeignAssetsInstance> for TestRuntime {
type StringLimit = AssetsStringLimit;
type WeightInfo = ();
}

parameter_types! {
pub const BlockHashCount: u32 = 250;
}
Expand Down Expand Up @@ -281,10 +285,6 @@ parameter_types! {
pub const MinVestedTransfer: u64 = 256 * 2;
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
pub PolimecReceiverInfo: XcmPalletInfo = XcmPalletInfo::new(
51, "PolimecReceiver".into(), "polimec_receiver".into(), 0, 1, 0
).unwrap();
}

#[cfg(feature = "runtime-benchmarks")]
parameter_types! {
Expand Down Expand Up @@ -406,6 +406,7 @@ parameter_types! {
pub const FeeRecipient: AccountId = 80085;
pub const RootId: PalletId = PalletId(*b"treasury");
}

impl pallet_proxy_bonding::Config for TestRuntime {
type BondingToken = Balances;
type BondingTokenDecimals = ConstU8<PLMC_DECIMALS>;
Expand Down
51 changes: 13 additions & 38 deletions runtimes/polimec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
fungible::{Credit, HoldConsideration, Inspect},
fungibles,
tokens::{self, ConversionToAssetBalance, PayFromAccount, UnityAssetBalanceConversion},
AsEnsureOriginWithArg, ConstU32, Contains, EitherOfDiverse, InstanceFilter, LinearStoragePrice, PrivilegeCmp,
AsEnsureOriginWithArg, ConstU32, EitherOfDiverse, Everything, InstanceFilter, LinearStoragePrice, PrivilegeCmp,
TransformOrigin,
},
weights::{ConstantMultiplier, Weight},
Expand Down Expand Up @@ -225,7 +225,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_version: 1_000_000,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 6,
transaction_version: 7,
state_version: 1,
};

Expand All @@ -240,38 +240,6 @@ parameter_types! {
pub const SS58Prefix: u16 = 41;
}

pub struct BaseCallFilter;
impl Contains<RuntimeCall> for BaseCallFilter {
fn contains(c: &RuntimeCall) -> bool {
match c {
RuntimeCall::Funding(call) =>
if cfg!(feature = "development-settings") {
true
} else {
matches!(
call,
pallet_funding::Call::create_project { .. } |
pallet_funding::Call::remove_project { .. } |
pallet_funding::Call::edit_project { .. } |
pallet_funding::Call::start_evaluation { .. } |
pallet_funding::Call::evaluate { .. } |
pallet_funding::Call::end_evaluation { .. } |
pallet_funding::Call::bid { .. } |
pallet_funding::Call::end_funding { .. } |
pallet_funding::Call::start_settlement { .. } |
pallet_funding::Call::settle_evaluation { .. } |
pallet_funding::Call::settle_bid { .. } |
pallet_funding::Call::mark_project_as_settled { .. } |
pallet_funding::Call::start_offchain_migration { .. } |
pallet_funding::Call::confirm_offchain_migration { .. } |
pallet_funding::Call::mark_project_ct_migration_as_finished { .. }
)
},
_ => true,
}
}
}

impl InstanceFilter<RuntimeCall> for Type {
fn filter(&self, c: &RuntimeCall) -> bool {
match self {
Expand Down Expand Up @@ -308,9 +276,7 @@ impl InstanceFilter<RuntimeCall> for Type {
RuntimeCall::Preimage(..) |
RuntimeCall::Scheduler(..)
),
proxy::Type::Staking => {
matches!(c, RuntimeCall::ParachainStaking(..))
},
proxy::Type::Staking => matches!(c, RuntimeCall::ParachainStaking(..)),
proxy::Type::IdentityJudgement =>
matches!(c, RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. })),
}
Expand All @@ -334,7 +300,7 @@ impl frame_system::Config for Runtime {
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
/// The basic call filter to use in dispatchable.
type BaseCallFilter = BaseCallFilter;
type BaseCallFilter = Everything;
/// The block type.
type Block = Block;
/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
Expand Down Expand Up @@ -1502,6 +1468,15 @@ impl_runtime_apis! {
fn contribution_tokens(account: AccountId) -> Vec<(ProjectId, Balance)> {
Funding::contribution_tokens(account)
}

fn evaluations_of(account: AccountId, project_id: Option<ProjectId>) -> Vec<EvaluationInfoOf<Runtime>> {
Funding::evaluations_of(account, project_id)
}


fn participations_of(account: AccountId, project_id: Option<ProjectId>) -> Vec<BidInfoOf<Runtime>> {
Funding::participations_of(account, project_id)
}
}

impl pallet_funding::functions::runtime_api::ProjectInformation<Block, Runtime> for Runtime {
Expand Down
Loading