multiversx_sdk/gateway/
gateway_account_esdt_roles.rs

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
use crate::data::esdt::EsdtRolesResponse;
use anyhow::anyhow;
use multiversx_chain_core::types::Address;
use std::collections::HashMap;

use super::{GatewayRequest, GatewayRequestType, ACCOUNT_ENDPOINT};

/// Retrieves an all esdt roles of an account from the network.
pub struct GetAccountEsdtRolesRequest<'a> {
    pub address: &'a Address,
}

impl<'a> GetAccountEsdtRolesRequest<'a> {
    pub fn new(address: &'a Address) -> Self {
        Self { address }
    }
}

impl GatewayRequest for GetAccountEsdtRolesRequest<'_> {
    type Payload = ();
    type DecodedJson = EsdtRolesResponse;
    type Result = HashMap<String, Vec<String>>;

    fn request_type(&self) -> GatewayRequestType {
        GatewayRequestType::Get
    }

    fn get_endpoint(&self) -> String {
        format!(
            "{ACCOUNT_ENDPOINT}/{}/esdts/roles",
            crate::bech32::encode(self.address)
        )
    }

    fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> {
        match decoded.data {
            None => Err(anyhow!("{}", decoded.error)),
            Some(b) => Ok(b.roles),
        }
    }
}