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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! RPC interface for the Portal pallet.

use std::sync::Arc;

use codec::Codec;
use jsonrpsee::{
    core::{Error as JsonRpseeError, RpcResult},
    proc_macros::rpc,
};
use sp_std::vec::Vec;

use pallet_portal_rpc_runtime_api::ChainId;
pub use pallet_portal_rpc_runtime_api::PortalRuntimeApi;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::traits::{Block as BlockT, MaybeDisplay};
use t3rn_types::sfx::SideEffect;

const RUNTIME_ERROR: i64 = 1;

#[rpc(client, server)]
pub trait PortalApi<AccountId, Balance, Hash> {
    /// Returns latest finalized header of a gateway if available
    #[method(name = "portal_fetchHeadHeight")]
    fn fetch_head_height(&self, chain_id: ChainId) -> RpcResult<u128>;

    #[method(name = "portal_allActiveXtx")]
    fn fetch_all_active_xtx(
        &self,
        for_executor: AccountId,
    ) -> RpcResult<
        Vec<(
            Hash,                                // xtx_id
            Vec<SideEffect<AccountId, Balance>>, // side_effects
            Vec<Hash>,                           // sfx_ids
        )>,
    >;
}

/// A struct that implements the [`PortalApi`].
pub struct Portal<C, P> {
    client: Arc<C>,
    _marker: std::marker::PhantomData<P>,
}

impl<C, P> Portal<C, P> {
    /// Create new `Portal` with the given reference to the client.
    pub fn new(client: Arc<C>) -> Self {
        Self {
            client,
            _marker: Default::default(),
        }
    }
}

impl<C, Block, AccountId, Balance, Hash> PortalApiServer<AccountId, Balance, Hash>
    for Portal<C, Block>
where
    AccountId: Codec + MaybeDisplay,
    Balance: Codec + MaybeDisplay,
    Hash: Codec + MaybeDisplay,
    Block: BlockT,
    C: ProvideRuntimeApi<Block> + HeaderBackend<Block> + Send + Sync + 'static,
    C::Api: PortalRuntimeApi<Block, AccountId, Balance, Hash>,
{
    // ToDo ChainId decoding is not working, like in XDNS
    fn fetch_head_height(&self, chain_id: ChainId) -> RpcResult<u128> {
        let api = self.client.runtime_api();
        let at = self.client.info().best_hash;

        let result: Option<u128> = api
            .fetch_head_height(at, chain_id)
            .map_err(runtime_error_into_rpc_err)?;

        match result {
            Some(height) => Ok(height),
            None => Err(runtime_error_into_rpc_err("ABI doesn't exist")),
        }
    }

    fn fetch_all_active_xtx(
        &self,
        for_executor: AccountId,
    ) -> RpcResult<
        Vec<(
            Hash,                                // xtx_id
            Vec<SideEffect<AccountId, Balance>>, // side_effects
            Vec<Hash>,                           // sfx_ids
        )>,
    > {
        let api = self.client.runtime_api();
        let at = self.client.info().best_hash;

        let result: Vec<(
            Hash,                                // xtx_id
            Vec<SideEffect<AccountId, Balance>>, // side_effects
            Vec<Hash>,                           // sfx_ids
        )> = api
            .fetch_all_active_xtx(at, for_executor)
            .map_err(runtime_error_into_rpc_err)?;

        Ok(result)
    }
}

fn runtime_error_into_rpc_err(err: impl std::fmt::Debug) -> JsonRpseeError {
    JsonRpseeError::Custom(format!("{err:?}"))
}