Skip to main content

erpc_sdk/
account.rs

1use serde::{Deserialize, Serialize};
2use tokio_util::sync::CancellationToken;
3
4use crate::{Result, rest::RestTransport};
5
6/// ERPC account plan returned by the token balance API.
7#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
8#[serde(rename_all = "lowercase")]
9pub enum ErpcPlan {
10    /// Business plan.
11    Business,
12    /// Developer plan.
13    Developer,
14    /// Free plan.
15    Free,
16    /// Pro plan.
17    Pro,
18}
19
20/// Current account token balance.
21#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
22pub struct TokenBalance {
23    /// Maximum token balance.
24    pub max_tokens: u64,
25    /// Next refill time, when applicable.
26    pub next_refill_at: Option<String>,
27    /// Current plan.
28    pub plan: ErpcPlan,
29    /// Remaining tokens.
30    pub remaining_tokens: u64,
31}
32
33/// Account and token-balance API.
34#[derive(Clone)]
35pub struct AccountClient {
36    transport: RestTransport,
37}
38
39impl AccountClient {
40    pub(crate) const fn new(transport: RestTransport) -> Self {
41        Self { transport }
42    }
43
44    /// Gets the current token balance.
45    pub async fn get_token_balance(&self) -> Result<TokenBalance> {
46        self.get_token_balance_with(None).await
47    }
48
49    /// Gets the current token balance with cancellation.
50    pub async fn get_token_balance_with(
51        &self,
52        cancellation: Option<&CancellationToken>,
53    ) -> Result<TokenBalance> {
54        self.transport
55            .get("/v3/erpc/token-balance", Vec::new(), cancellation)
56            .await
57    }
58}