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
//! Runtime API for Stream Payment pallet
18

            
19
#![cfg_attr(not(feature = "std"), no_std)]
20

            
21
extern crate alloc;
22

            
23
use {
24
    alloc::string::String,
25
    parity_scale_codec::{Decode, Encode},
26
    serde::{Deserialize, Serialize},
27
};
28

            
29
#[derive(
30
1584
    Debug, Copy, Clone, PartialEq, Eq, Encode, Decode, scale_info::TypeInfo, Serialize, Deserialize,
31
)]
32
pub struct StreamPaymentApiStatus<Balance> {
33
    pub payment: Balance,
34
    pub deposit_left: Balance,
35
    /// Whenever the stream is stalled, which can occur either when no funds are left or
36
    /// if the time is past a mandatory request deadline.
37
    pub stalled: bool,
38
}
39

            
40
1056
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, scale_info::TypeInfo)]
41
#[cfg_attr(feature = "std", derive(thiserror::Error))]
42
pub enum StreamPaymentApiError {
43
4
    #[cfg_attr(feature = "std", error("Unknown stream id"))]
44
    UnknownStreamId,
45
    #[cfg_attr(feature = "std", error("Other error: {0}"))]
46
    Other(String),
47
}
48

            
49
636
sp_api::decl_runtime_apis! {
50
636
    pub trait StreamPaymentApi<StreamId, Instant, Balance>
51
636
    where
52
636
        StreamId: parity_scale_codec::Codec,
53
636
        Instant: parity_scale_codec::Codec,
54
636
        Balance: parity_scale_codec::Codec,
55
636
    {
56
636
        /// Get the stream payment current status, telling how much payment is
57
636
        /// pending, how much deposit will be left and whenever the stream is stalled.
58
636
        /// The stream is considered stalled if no funds are left or if the provided
59
636
        /// time is past a mandatory request deadline. If the provided `now` is `None`
60
636
        /// then the current time will be fetched. Being able to provide a custom `now`
61
636
        /// allows to check the status in the future.
62
636
        fn stream_payment_status(
63
636
            stream_id: StreamId,
64
636
            now: Option<Instant>,
65
636
        ) -> Result<StreamPaymentApiStatus<Balance>, StreamPaymentApiError>;
66
636
    }
67
654
}