Skip to content

Commit ec125d4

Browse files
authored
✨ Runtime API to retrieve evaluation/participation made by a user (#463)
## What and Why? Added two new runtime API endpoints to the `UserInformation`: 1. `evaluations_of`: Retrieves all evaluations made by a user, with optional filtering by project id. 2. `participations_of`: Retrieves all bids made by a user, with optional filtering by project id. ## Also - Restored the old `pallet_scheduler` weights, as per polkadot-fellows/runtimes#614 - Bumped `UserInformation` API version from 1 to 2 to reflect these additions - Updated Rust toolchain from 1.81.0 to 1.84.1. So we use the same Rust version used by `srtool` (https://github.com/paritytech/srtool/releases/tag/v0.18.2) - Removed `--no-wasm-std` flag from `srtool` build commands
2 parents e946b01 + 0c19e33 commit ec125d4

File tree

6 files changed

+107
-100
lines changed

6 files changed

+107
-100
lines changed

justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ default:
44

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

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

1212
# Test the runtimes features
1313
test-runtime-features runtime="polimec-runtime":

pallets/funding/src/functions/runtime_api.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ sp_api::decl_runtime_apis! {
2323
fn top_projects_by_usd_target_percent_reached(amount: u32) -> Vec<(ProjectId, ProjectMetadataOf<T>, ProjectDetailsOf<T>)>;
2424
}
2525

26-
#[api_version(1)]
26+
#[api_version(2)]
2727
pub trait UserInformation<T: Config> {
28-
/// Get all the contribution token balances for the participated projects
28+
/// Get all the contribution token balances for the participated projects.
2929
fn contribution_tokens(account: AccountIdOf<T>) -> Vec<(ProjectId, Balance)>;
30+
31+
/// Get all the `EvaluationInfoOf` made by a single account, for a specific project if provided.
32+
fn evaluations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<EvaluationInfoOf<T>>;
33+
34+
/// Get all the `BidInfoOf` made by a single account, for a specific project if provided.
35+
fn participations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<BidInfoOf<T>>;
3036
}
3137

3238
#[api_version(1)]
@@ -82,6 +88,30 @@ impl<T: Config> Pallet<T> {
8288
.collect_vec()
8389
}
8490

91+
pub fn participations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<BidInfoOf<T>> {
92+
match project_id {
93+
Some(id) => Bids::<T>::iter_prefix_values(id).filter(|bid| bid.bidder == account).collect_vec(),
94+
None => Bids::<T>::iter_values().filter(|bid| bid.bidder == account).collect_vec(),
95+
}
96+
}
97+
98+
pub fn evaluations_of(account: AccountIdOf<T>, project_id: Option<ProjectId>) -> Vec<EvaluationInfoOf<T>> {
99+
match project_id {
100+
Some(id) => {
101+
// Use both project ID and account as prefix
102+
let prefix = (id, account);
103+
Evaluations::<T>::iter_prefix_values(prefix).collect_vec()
104+
},
105+
None => {
106+
// If no project is specified, iterate over all projects for this account
107+
Evaluations::<T>::iter()
108+
.filter(|((_, evaluator, _), _)| *evaluator == account)
109+
.map(|(_, value)| value)
110+
.collect_vec()
111+
},
112+
}
113+
}
114+
85115
pub fn top_projects_by_usd_raised(amount: u32) -> Vec<(ProjectId, ProjectMetadataOf<T>, ProjectDetailsOf<T>)> {
86116
ProjectsDetails::<T>::iter()
87117
.sorted_by(|a, b| b.1.funding_amount_reached_usd.cmp(&a.1.funding_amount_reached_usd))

pallets/funding/src/mock.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ parameter_types! {
155155
pub const ApprovalDeposit: Balance = EXISTENTIAL_DEPOSIT;
156156

157157
}
158+
158159
impl pallet_assets::Config<ContributionTokensInstance> for TestRuntime {
159160
type ApprovalDeposit = ApprovalDeposit;
160161
type AssetAccountDeposit = ZeroAssetAccountDeposit;
@@ -180,12 +181,14 @@ impl pallet_assets::Config<ContributionTokensInstance> for TestRuntime {
180181

181182
#[cfg(feature = "runtime-benchmarks")]
182183
pub struct PalletAssetsBenchmarkHelper;
184+
183185
#[cfg(feature = "runtime-benchmarks")]
184186
impl pallet_assets::BenchmarkHelper<Location> for PalletAssetsBenchmarkHelper {
185187
fn create_asset_id_parameter(id: u32) -> Location {
186188
(Parent, Parachain(id)).into()
187189
}
188190
}
191+
189192
impl pallet_assets::Config<ForeignAssetsInstance> for TestRuntime {
190193
type ApprovalDeposit = ApprovalDeposit;
191194
type AssetAccountDeposit = AssetAccountDeposit;
@@ -208,6 +211,7 @@ impl pallet_assets::Config<ForeignAssetsInstance> for TestRuntime {
208211
type StringLimit = AssetsStringLimit;
209212
type WeightInfo = ();
210213
}
214+
211215
parameter_types! {
212216
pub const BlockHashCount: u32 = 250;
213217
}
@@ -281,10 +285,6 @@ parameter_types! {
281285
pub const MinVestedTransfer: u64 = 256 * 2;
282286
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
283287
WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE);
284-
pub PolimecReceiverInfo: XcmPalletInfo = XcmPalletInfo::new(
285-
51, "PolimecReceiver".into(), "polimec_receiver".into(), 0, 1, 0
286-
).unwrap();
287-
}
288288

289289
#[cfg(feature = "runtime-benchmarks")]
290290
parameter_types! {
@@ -406,6 +406,7 @@ parameter_types! {
406406
pub const FeeRecipient: AccountId = 80085;
407407
pub const RootId: PalletId = PalletId(*b"treasury");
408408
}
409+
409410
impl pallet_proxy_bonding::Config for TestRuntime {
410411
type BondingToken = Balances;
411412
type BondingTokenDecimals = ConstU8<PLMC_DECIMALS>;

runtimes/polimec/src/lib.rs

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use frame_support::{
3232
fungible::{Credit, HoldConsideration, Inspect},
3333
fungibles,
3434
tokens::{self, ConversionToAssetBalance, PayFromAccount, UnityAssetBalanceConversion},
35-
AsEnsureOriginWithArg, ConstU32, Contains, EitherOfDiverse, InstanceFilter, LinearStoragePrice, PrivilegeCmp,
35+
AsEnsureOriginWithArg, ConstU32, EitherOfDiverse, Everything, InstanceFilter, LinearStoragePrice, PrivilegeCmp,
3636
TransformOrigin,
3737
},
3838
weights::{ConstantMultiplier, Weight},
@@ -225,7 +225,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
225225
spec_version: 1_000_000,
226226
impl_version: 0,
227227
apis: RUNTIME_API_VERSIONS,
228-
transaction_version: 6,
228+
transaction_version: 7,
229229
state_version: 1,
230230
};
231231

@@ -240,38 +240,6 @@ parameter_types! {
240240
pub const SS58Prefix: u16 = 41;
241241
}
242242

243-
pub struct BaseCallFilter;
244-
impl Contains<RuntimeCall> for BaseCallFilter {
245-
fn contains(c: &RuntimeCall) -> bool {
246-
match c {
247-
RuntimeCall::Funding(call) =>
248-
if cfg!(feature = "development-settings") {
249-
true
250-
} else {
251-
matches!(
252-
call,
253-
pallet_funding::Call::create_project { .. } |
254-
pallet_funding::Call::remove_project { .. } |
255-
pallet_funding::Call::edit_project { .. } |
256-
pallet_funding::Call::start_evaluation { .. } |
257-
pallet_funding::Call::evaluate { .. } |
258-
pallet_funding::Call::end_evaluation { .. } |
259-
pallet_funding::Call::bid { .. } |
260-
pallet_funding::Call::end_funding { .. } |
261-
pallet_funding::Call::start_settlement { .. } |
262-
pallet_funding::Call::settle_evaluation { .. } |
263-
pallet_funding::Call::settle_bid { .. } |
264-
pallet_funding::Call::mark_project_as_settled { .. } |
265-
pallet_funding::Call::start_offchain_migration { .. } |
266-
pallet_funding::Call::confirm_offchain_migration { .. } |
267-
pallet_funding::Call::mark_project_ct_migration_as_finished { .. }
268-
)
269-
},
270-
_ => true,
271-
}
272-
}
273-
}
274-
275243
impl InstanceFilter<RuntimeCall> for Type {
276244
fn filter(&self, c: &RuntimeCall) -> bool {
277245
match self {
@@ -308,9 +276,7 @@ impl InstanceFilter<RuntimeCall> for Type {
308276
RuntimeCall::Preimage(..) |
309277
RuntimeCall::Scheduler(..)
310278
),
311-
proxy::Type::Staking => {
312-
matches!(c, RuntimeCall::ParachainStaking(..))
313-
},
279+
proxy::Type::Staking => matches!(c, RuntimeCall::ParachainStaking(..)),
314280
proxy::Type::IdentityJudgement =>
315281
matches!(c, RuntimeCall::Identity(pallet_identity::Call::provide_judgement { .. })),
316282
}
@@ -334,7 +300,7 @@ impl frame_system::Config for Runtime {
334300
/// The identifier used to distinguish between accounts.
335301
type AccountId = AccountId;
336302
/// The basic call filter to use in dispatchable.
337-
type BaseCallFilter = BaseCallFilter;
303+
type BaseCallFilter = Everything;
338304
/// The block type.
339305
type Block = Block;
340306
/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
@@ -1502,6 +1468,15 @@ impl_runtime_apis! {
15021468
fn contribution_tokens(account: AccountId) -> Vec<(ProjectId, Balance)> {
15031469
Funding::contribution_tokens(account)
15041470
}
1471+
1472+
fn evaluations_of(account: AccountId, project_id: Option<ProjectId>) -> Vec<EvaluationInfoOf<Runtime>> {
1473+
Funding::evaluations_of(account, project_id)
1474+
}
1475+
1476+
1477+
fn participations_of(account: AccountId, project_id: Option<ProjectId>) -> Vec<BidInfoOf<Runtime>> {
1478+
Funding::participations_of(account, project_id)
1479+
}
15051480
}
15061481

15071482
impl pallet_funding::functions::runtime_api::ProjectInformation<Block, Runtime> for Runtime {

0 commit comments

Comments
 (0)