multiversx_sdk/gateway/
gateway_account_esdt_tokens.rs

1use crate::data::esdt::{EsdtBalance, EsdtBalanceResponse};
2use anyhow::anyhow;
3use multiversx_chain_core::std::Bech32Address;
4use std::collections::HashMap;
5
6use super::{GatewayRequest, GatewayRequestType, ACCOUNT_ENDPOINT};
7
8/// Retrieves an all esdt tokens of an account from the network.
9pub struct GetAccountEsdtTokensRequest<'a> {
10    pub address: &'a Bech32Address,
11}
12
13impl<'a> GetAccountEsdtTokensRequest<'a> {
14    pub fn new(address: &'a Bech32Address) -> Self {
15        Self { address }
16    }
17}
18
19impl GatewayRequest for GetAccountEsdtTokensRequest<'_> {
20    type Payload = ();
21    type DecodedJson = EsdtBalanceResponse;
22    type Result = HashMap<String, EsdtBalance>;
23
24    fn request_type(&self) -> GatewayRequestType {
25        GatewayRequestType::Get
26    }
27
28    fn get_endpoint(&self) -> String {
29        format!("{ACCOUNT_ENDPOINT}/{}/esdt", 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.esdts),
36        }
37    }
38}