casper_client/rpcs/v2_0_0/
get_entity.rs

1use casper_types::{
2    account::{Account, AccountHash},
3    AddressableEntity as CasperTypesAddressableEntity, EntityAddr, EntryPointValue, NamedKeys,
4    ProtocolVersion, PublicKey,
5};
6use serde::{Deserialize, Serialize};
7
8use crate::rpcs::common::BlockIdentifier;
9
10pub(crate) const GET_ENTITY_METHOD: &str = "state_get_entity";
11
12/// Identifier of an addressable entity.
13#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Clone)]
14#[serde(deny_unknown_fields)]
15pub enum EntityIdentifier {
16    /// The public key of an account.
17    PublicKey(PublicKey),
18    /// The account hash of an account.
19    AccountHash(AccountHash),
20    /// The address of an addressable entity.
21    EntityAddr(EntityAddr),
22}
23
24/// Represents an entity that is addressable within the Casper system.
25/// This struct holds the entity, its associated named keys, and its
26/// entry points for interaction.
27///
28/// # Fields
29///
30/// * `entity` - The addressable entity which could be a contract, account, or other entity types in Casper.
31/// * `named_keys` - A collection of named keys that are associated with the entity. Named keys
32///   are a mapping from string identifiers to keys (e.g., contracts, URefs, etc.).
33/// * `entry_points` - A list of entry points representing methods or functions that the entity exposes.
34///   Entry points define the public interface of a contract or other executable object.
35#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
36pub struct AddressableEntity {
37    /// The core addressable entity in Casper (account, contract, etc.).
38    pub entity: CasperTypesAddressableEntity,
39
40    /// The named keys associated with the entity, mapping identifiers to actual keys.
41    pub named_keys: NamedKeys,
42
43    /// A collection of entry points for the entity, defining its public interface.
44    pub entry_points: Vec<EntryPointValue>,
45}
46
47/// An addressable entity or a legacy account.
48#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
49pub enum EntityOrAccount {
50    /// An addressable entity with named keys and entry points.
51    AddressableEntity(AddressableEntity),
52    /// A legacy account.
53    LegacyAccount(Account),
54}
55
56impl EntityOrAccount {
57    /// Returns the addressable entity if present.
58    pub fn addressable_entity(&self) -> Option<&AddressableEntity> {
59        if let EntityOrAccount::AddressableEntity(addressable_entity) = &self {
60            Some(addressable_entity)
61        } else {
62            None
63        }
64    }
65
66    /// Returns the legacy account if present.
67    pub fn legacy_account(&self) -> Option<&Account> {
68        if let EntityOrAccount::LegacyAccount(account) = &self {
69            Some(account)
70        } else {
71            None
72        }
73    }
74}
75
76/// Params for "state_get_entity" RPC request
77#[derive(Serialize, Deserialize, Debug)]
78#[serde(deny_unknown_fields)]
79pub(crate) struct GetAddressableEntityParams {
80    /// The identifier of the entity.
81    entity_identifier: EntityIdentifier,
82    /// The block identifier.
83    block_identifier: Option<BlockIdentifier>,
84}
85
86impl GetAddressableEntityParams {
87    /// Returns a new `GetAddressableEntityParams`.
88    pub fn new(
89        entity_identifier: EntityIdentifier,
90        block_identifier: Option<BlockIdentifier>,
91    ) -> Self {
92        GetAddressableEntityParams {
93            entity_identifier,
94            block_identifier,
95        }
96    }
97}
98
99/// Result for "state_get_entity" RPC response.
100#[derive(PartialEq, Eq, Serialize, Deserialize, Debug, Clone)]
101#[serde(deny_unknown_fields)]
102pub struct GetAddressableEntityResult {
103    /// The RPC API version.
104    pub api_version: ProtocolVersion,
105    /// The addressable entity or a legacy account.
106    #[serde(alias = "entity")]
107    pub entity_result: EntityOrAccount,
108    /// The Merkle proof.
109    pub merkle_proof: String,
110}