1
// Copyright (C) Moondance Labs Ltd.
2
// This file is part of Tanssi.
3

            
4
// Tanssi is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Tanssi is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Tanssi.  If not, see <http://www.gnu.org/licenses/>
16

            
17
//! RPC client for Services Payment pallet
18

            
19
pub use pallet_services_payment_runtime_api::ServicesPaymentApi as ServicesPaymentRuntimeApi;
20
use {
21
    core::marker::PhantomData,
22
    jsonrpsee::{
23
        core::{async_trait, RpcResult},
24
        proc_macros::rpc,
25
    },
26
    sc_client_api::UsageProvider,
27
    sp_api::ProvideRuntimeApi,
28
    sp_runtime::traits::Block as BlockT,
29
    std::sync::Arc,
30
};
31

            
32
400
#[rpc(server)]
33
pub trait ServicesPaymentApi<Balance, ParaId> {
34
    #[method(name = "tanssi_servicesPaymentBlockCost")]
35
    async fn block_cost(&self, para_id: ParaId) -> RpcResult<Balance>;
36

            
37
    #[method(name = "tanssi_servicesPaymentCollatorAssignmentCost")]
38
    async fn collator_assignment_cost(&self, para_id: ParaId) -> RpcResult<Balance>;
39
}
40

            
41
pub struct ServicesPayment<Client, Block> {
42
    client: Arc<Client>,
43
    _phantom: PhantomData<Block>,
44
}
45

            
46
impl<Client, Block> ServicesPayment<Client, Block> {
47
388
    pub fn new(client: Arc<Client>) -> Self {
48
388
        Self {
49
388
            client,
50
388
            _phantom: PhantomData,
51
388
        }
52
388
    }
53
}
54

            
55
#[async_trait]
56
impl<Client, Hash, Block, Balance, ParaId> ServicesPaymentApiServer<Balance, ParaId>
57
    for ServicesPayment<Client, Block>
58
where
59
    Hash: Send + 'static,
60
    Block: BlockT<Hash = Hash>,
61
    Client: ProvideRuntimeApi<Block> + Sync + Send + UsageProvider<Block> + 'static,
62
    Client::Api: ServicesPaymentRuntimeApi<Block, Balance, ParaId>,
63
    Balance: parity_scale_codec::Codec + Send + 'static,
64
    ParaId: parity_scale_codec::Codec + Send + 'static,
65
{
66
2
    async fn block_cost(&self, para_id: ParaId) -> RpcResult<Balance> {
67
2
        let cost = self
68
2
            .client
69
2
            .runtime_api()
70
2
            .block_cost(self.client.usage_info().chain.best_hash, para_id)
71
2
            .map_err(internal_err)?;
72
2
        Ok(cost)
73
4
    }
74

            
75
2
    async fn collator_assignment_cost(&self, para_id: ParaId) -> RpcResult<Balance> {
76
2
        let cost = self
77
2
            .client
78
2
            .runtime_api()
79
2
            .collator_assignment_cost(self.client.usage_info().chain.best_hash, para_id)
80
2
            .map_err(internal_err)?;
81
2
        Ok(cost)
82
4
    }
83
}
84

            
85
pub fn internal_err<T: ToString>(message: T) -> jsonrpsee::types::ErrorObjectOwned {
86
    jsonrpsee::types::error::ErrorObject::borrowed(
87
        jsonrpsee::types::error::INTERNAL_ERROR_CODE,
88
        &message.to_string(),
89
        None,
90
    )
91
    .into_owned()
92
}