near_api/types/
reference.rs

1// Source: <https://github.com/near/near-workspaces-rs/blob/10a6c1a00b2b6c937242043312455e05f0d4a125/workspaces/src/types/mod.rs#L513C1-L537C2>
2
3use crate::types::CryptoHash;
4use near_primitives::types::{BlockHeight, EpochId};
5
6/// A reference to a specific block. This type is used to specify the block for most queries.
7///
8/// It represents the finality of a transaction or block in which transaction is included in. For more info
9/// go to the [NEAR finality](https://docs.near.org/docs/concepts/transaction#finality) docs.
10#[derive(Clone, Debug)]
11#[non_exhaustive]
12pub enum Reference {
13    /// Optimistic finality. The latest block recorded on the node that responded to our query
14    /// (<1 second delay after the transaction is submitted).
15    Optimistic,
16    /// Near-final finality. Similarly to `Final` finality, but delay should be roughly 1 second.
17    DoomSlug,
18    /// Final finality. The block that has been validated on at least 66% of the nodes in the
19    /// network. (At max, should be 2 second delay after the transaction is submitted.)
20    Final,
21    /// Reference to a specific block.
22    AtBlock(BlockHeight),
23    /// Reference to a specific block hash.
24    AtBlockHash(CryptoHash),
25}
26
27impl From<Reference> for near_primitives::types::BlockReference {
28    fn from(value: Reference) -> Self {
29        match value {
30            Reference::Optimistic => near_primitives::types::Finality::None.into(),
31            Reference::DoomSlug => near_primitives::types::Finality::DoomSlug.into(),
32            Reference::Final => near_primitives::types::Finality::Final.into(),
33            Reference::AtBlock(block_height) => {
34                near_primitives::types::BlockId::Height(block_height).into()
35            }
36            Reference::AtBlockHash(block_hash) => {
37                near_primitives::types::BlockId::Hash(block_hash.into()).into()
38            }
39        }
40    }
41}
42
43/// A reference to a specific epoch. This type is used to specify the epoch for some queries.
44#[derive(Clone, Debug)]
45#[non_exhaustive]
46pub enum EpochReference {
47    /// Reference to a specific Epoch Id
48    AtEpoch(CryptoHash),
49    /// Reference to an epoch at a specific block height.
50    AtBlock(BlockHeight),
51    /// Reference to an epoch at a specific block hash.
52    AtBlockHash(CryptoHash),
53    /// Latest epoch on the node
54    Latest,
55}
56
57impl From<EpochReference> for near_primitives::types::EpochReference {
58    fn from(value: EpochReference) -> Self {
59        match value {
60            EpochReference::AtBlock(block_height) => {
61                Self::BlockId(near_primitives::types::BlockId::Height(block_height))
62            }
63            EpochReference::AtBlockHash(block_hash) => {
64                Self::BlockId(near_primitives::types::BlockId::Hash(block_hash.into()))
65            }
66            EpochReference::AtEpoch(epoch) => Self::EpochId(EpochId(epoch.into())),
67            EpochReference::Latest => Self::Latest,
68        }
69    }
70}