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
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_profiles::rpc::FlatSocialAccount;
use pallet_utils::rpc::map_rpc_error;
pub use profiles_runtime_api::ProfilesApi as ProfilesRuntimeApi;

#[rpc]
pub trait ProfilesApi<BlockHash, AccountId, BlockNumber> {
    #[rpc(name = "profiles_getSocialAccountsByIds")]
    fn get_social_accounts_by_ids(
        &self,
        at: Option<BlockHash>,
        account_ids: Vec<AccountId>,
    ) -> Result<Vec<FlatSocialAccount<AccountId, BlockNumber>>>;
}

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

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

impl<C, Block, AccountId, BlockNumber> ProfilesApi<<Block as BlockT>::Hash, AccountId, BlockNumber>
    for Profiles<C, Block>
where
    Block: BlockT,
    AccountId: Codec,
    BlockNumber: Codec,
    C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
    C::Api: ProfilesRuntimeApi<Block, AccountId, BlockNumber>,
{
    fn get_social_accounts_by_ids(&self, at: Option<<Block as BlockT>::Hash>, account_ids: Vec<AccountId>) -> Result<Vec<FlatSocialAccount<AccountId, BlockNumber>>> {
        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_social_accounts_by_ids(&at, account_ids);
        runtime_api_result.map_err(map_rpc_error)
    }
}