1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::sync::Arc;
use codec::Codec;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use jsonrpc_core::Result;
use jsonrpc_derive::rpc;
use sp_api::ProvideRuntimeApi;

use pallet_utils::{SpaceId, rpc::map_rpc_error};
pub use space_follows_runtime_api::SpaceFollowsApi as SpaceFollowsRuntimeApi;

#[rpc]
pub trait SpaceFollowsApi<BlockHash, AccountId> {
    #[rpc(name = "spaceFollows_getSpaceIdsFollowedByAccount")]
    fn get_space_ids_followed_by_account(
        &self,
        at: Option<BlockHash>,
        account: AccountId,
    ) -> Result<Vec<SpaceId>>;

    #[rpc(name = "spaceFollows_filterFollowedSpaceIds")]
    fn filter_followed_space_ids(
        &self,
        at: Option<BlockHash>,
        account: AccountId,
        space_ids: Vec<SpaceId>,
    ) -> Result<Vec<SpaceId>>;
}

pub struct SpaceFollows<C, M> {
    client: Arc<C>,
    _marker: std::marker::PhantomData<M>,
}

impl<C, M> SpaceFollows<C, M> {
    pub fn new(client: Arc<C>) -> Self {
        Self {
            client,
            _marker: Default::default(),
        }
    }
}

impl<C, Block, AccountId> SpaceFollowsApi<<Block as BlockT>::Hash, AccountId>
    for SpaceFollows<C, Block>
where
    Block: BlockT,
    AccountId: Codec,
    C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
    C::Api: SpaceFollowsRuntimeApi<Block, AccountId>,
{
    fn get_space_ids_followed_by_account(
        &self,
        at: Option<<Block as BlockT>::Hash>,
        account: AccountId,
    ) -> Result<Vec<SpaceId>> {
        let api = self.client.runtime_api();
        let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

        let runtime_api_result = api.get_space_ids_followed_by_account(&at, account);
        runtime_api_result.map_err(map_rpc_error)
    }

    fn filter_followed_space_ids(
        &self,
        at: Option<<Block as BlockT>::Hash>,
        account: AccountId,
        space_ids: Vec<u64>,
    ) -> Result<Vec<SpaceId>> {
        let api = self.client.runtime_api();
        let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));

        let runtime_api_result = api.filter_followed_space_ids(&at, account, space_ids);
        runtime_api_result.map_err(map_rpc_error)
    }
}