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
67
68
69
70
71
72
//! Environment query-related types.
use crate::{address::Address, token::Denomination};

/// A query request.
#[non_exhaustive]
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum QueryRequest {
    /// Information about the current runtime block.
    #[cbor(rename = "block_info")]
    BlockInfo,

    /// Accounts queries.
    #[cbor(rename = "accounts")]
    Accounts(AccountsQuery),
}

/// A query response.
#[non_exhaustive]
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum QueryResponse {
    /// Indication of a failing request.
    #[cbor(rename = "error")]
    Error {
        module: String,
        code: u32,
        message: String,
    },

    /// Information about the current runtime block.
    #[cbor(rename = "block_info")]
    BlockInfo {
        round: u64,
        epoch: u64,
        timestamp: u64,
    },

    /// Accounts queries.
    #[cbor(rename = "accounts")]
    Accounts(AccountsResponse),
}

/// Accounts API queries.
#[non_exhaustive]
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum AccountsQuery {
    /// Query an account's balance.
    #[cbor(rename = "balance")]
    Balance {
        address: Address,
        denomination: Denomination,
    },
}

impl From<AccountsQuery> for QueryRequest {
    fn from(q: AccountsQuery) -> Self {
        Self::Accounts(q)
    }
}

/// Accounts API responses.
#[non_exhaustive]
#[derive(Clone, Debug, cbor::Encode, cbor::Decode)]
pub enum AccountsResponse {
    /// An account's balance of the given denomination.
    Balance { balance: u128 },
}

impl From<AccountsResponse> for QueryResponse {
    fn from(q: AccountsResponse) -> Self {
        Self::Accounts(q)
    }
}