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
1494
    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
996
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, scale_info::TypeInfo)]
41
12
#[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
600
sp_api::decl_runtime_apis! {
50
600
    pub trait StreamPaymentApi<StreamId, Instant, Balance>
51
600
    where
52
600
        StreamId: parity_scale_codec::Codec,
53
600
        Instant: parity_scale_codec::Codec,
54
600
        Balance: parity_scale_codec::Codec,
55
600
    {
56
600
        /// Get the stream payment current status, telling how much payment is
57
600
        /// pending, how much deposit will be left and whenever the stream is stalled.
58
600
        /// The stream is considered stalled if no funds are left or if the provided
59
600
        /// time is past a mandatory request deadline. If the provided `now` is `None`
60
600
        /// then the current time will be fetched. Being able to provide a custom `now`
61
600
        /// allows to check the status in the future.
62
600
        fn stream_payment_status(
63
600
            stream_id: StreamId,
64
600
            now: Option<Instant>,
65
600
        ) -> Result<StreamPaymentApiStatus<Balance>, StreamPaymentApiError>;
66
600
    }
67
600
}