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
};
27

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

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

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