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
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

use super::runtime_types::RuntimeApi;

use crate::{
    client::OnlineClientT,
    error::Error,
    Config,
};
use derivative::Derivative;
use std::{
    future::Future,
    marker::PhantomData,
};

/// Execute runtime API calls.
#[derive(Derivative)]
#[derivative(Clone(bound = "Client: Clone"))]
pub struct RuntimeApiClient<T, Client> {
    client: Client,
    _marker: PhantomData<T>,
}

impl<T, Client> RuntimeApiClient<T, Client> {
    /// Create a new [`RuntimeApiClient`]
    pub fn new(client: Client) -> Self {
        Self {
            client,
            _marker: PhantomData,
        }
    }
}

impl<T, Client> RuntimeApiClient<T, Client>
where
    T: Config,
    Client: OnlineClientT<T>,
{
    /// Obtain a runtime API at some block hash.
    pub fn at(
        &self,
        block_hash: Option<T::Hash>,
    ) -> impl Future<Output = Result<RuntimeApi<T, Client>, Error>> + Send + 'static {
        // Clone and pass the client in like this so that we can explicitly
        // return a Future that's Send + 'static, rather than tied to &self.
        let client = self.client.clone();
        async move {
            // If block hash is not provided, get the hash
            // for the latest block and use that.
            let block_hash = match block_hash {
                Some(hash) => hash,
                None => {
                    client
                        .rpc()
                        .block_hash(None)
                        .await?
                        .expect("substrate RPC returns the best block when no block number is provided; qed")
                }
            };

            Ok(RuntimeApi::new(client, block_hash))
        }
    }
}