multiversx_sdk/gateway/
gateway_account.rs

1use crate::data::account::{Account, AccountResponse};
2use anyhow::anyhow;
3use multiversx_chain_core::std::Bech32Address;
4
5use super::ACCOUNT_ENDPOINT;
6use super::{GatewayRequest, GatewayRequestType};
7
8/// Retrieves an account info from the network (nonce, balance).
9pub struct GetAccountRequest<'a> {
10    pub address: &'a Bech32Address,
11}
12
13impl<'a> GetAccountRequest<'a> {
14    pub fn new(address: &'a Bech32Address) -> Self {
15        Self { address }
16    }
17}
18
19impl GatewayRequest for GetAccountRequest<'_> {
20    type Payload = ();
21    type DecodedJson = AccountResponse;
22    type Result = Account;
23
24    fn request_type(&self) -> GatewayRequestType {
25        GatewayRequestType::Get
26    }
27
28    fn get_endpoint(&self) -> String {
29        format!("{ACCOUNT_ENDPOINT}/{}", self.address.bech32)
30    }
31
32    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
33        match decoded.data {
34            None => Err(anyhow!("{}", decoded.error)),
35            Some(b) => Ok(b.account),
36        }
37    }
38}