Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "3"
members = ["api", "types"]

[workspace.package]
rust-version = "1.86"
rust-version = "1.88"
edition = "2024"
version = "0.8.5"
authors = [
Expand Down Expand Up @@ -55,8 +55,7 @@ near-gas = { version = "0.3", features = ["serde", "borsh"] }
near-token = { version = "0.3", features = ["serde", "borsh"] }
near-abi = "0.4.2"
near-ledger = "0.9.1"
near-openapi-client = "0.8"
near-openapi-types = "0.8"
near-openrpc-client = { version = "0.1.0", default-features = false }

# Dev-dependencies
near-primitives = { version = "0.34" }
Expand Down
3 changes: 1 addition & 2 deletions api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ openssl = { workspace = true, features = ["vendored"] }
bip39 = { workspace = true, features = ["rand"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
serde_dbgfmt.workspace = true
slipped10.workspace = true
url.workspace = true
tokio = { workspace = true, default-features = false, features = ["time"] }
tracing.workspace = true
thiserror.workspace = true

near-ledger = { workspace = true, optional = true }
near-openapi-client.workspace = true
near-openrpc-client.workspace = true
near-api-types.workspace = true

zstd.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion api/src/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Account {
crate::stake::Delegation(self.0.clone())
}

/// Prepares a query to fetch the [Data](crate::Data)<[AccountView](near_api_types::AccountView)> with the account information for the given account ID.
/// Prepares a query to fetch the [Data](crate::Data)<[Account](near_api_types::Account)> with the account information for the given account ID.
///
/// ## Example
/// ```rust,no_run
Expand Down
6 changes: 3 additions & 3 deletions api/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use near_api_types::{BlockHeight, CryptoHash, Reference};

use crate::{
advanced::block_rpc::SimpleBlockRpc,
common::query::{PostprocessHandler, RequestBuilder, RpcBlockHandler},
common::query::{AndThenHandler, PostprocessHandler, RequestBuilder, RpcBlockHandler},
};

/// Chain-related interactions with the NEAR Protocol
Expand Down Expand Up @@ -79,9 +79,9 @@ impl Chain {
/// # Ok(())
/// # }
/// ```
pub fn block_hash() -> RequestBuilder<PostprocessHandler<CryptoHash, RpcBlockHandler>> {
pub fn block_hash() -> RequestBuilder<AndThenHandler<CryptoHash, RpcBlockHandler>> {
RequestBuilder::new(SimpleBlockRpc, Reference::Optimistic, RpcBlockHandler)
.map(|data| CryptoHash::from(data.header.hash))
.and_then(|data| Ok(CryptoHash::try_from(data.header.hash)?))
}

/// Set ups a query to fetch the [RpcBlockResponse][near_api_types::RpcBlockResponse]
Expand Down
56 changes: 10 additions & 46 deletions api/src/common/query/block_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
use near_api_types::Reference;
use near_openapi_client::Client;
use near_openapi_client::types::{
BlockId, ErrorWrapperForRpcBlockError, Finality, JsonRpcRequestForBlock,
JsonRpcRequestForBlockMethod, JsonRpcResponseForRpcBlockResponseAndRpcBlockError,
RpcBlockError, RpcBlockRequest, RpcBlockResponse,
};
use near_openrpc_client::{BlockId, Finality, RpcBlockRequest, RpcBlockResponse};

use crate::common::utils::to_retry_error;
use crate::{
NetworkConfig, advanced::RpcType, common::utils::is_critical_blocks_error,
config::RetryResponse, errors::SendRequestError,
NetworkConfig, advanced::RpcType, common::utils::is_critical_rpc_error, config::RetryResponse,
errors::SendRequestError, rpc_client::RpcClient,
};

#[derive(Clone, Debug)]
Expand All @@ -19,13 +14,12 @@ pub struct SimpleBlockRpc;
impl RpcType for SimpleBlockRpc {
type RpcReference = Reference;
type Response = RpcBlockResponse;
type Error = RpcBlockError;
async fn send_query(
&self,
client: &Client,
client: &RpcClient,
_network: &NetworkConfig,
reference: &Reference,
) -> RetryResponse<RpcBlockResponse, SendRequestError<RpcBlockError>> {
) -> RetryResponse<RpcBlockResponse, SendRequestError> {
let request = match reference {
Reference::Optimistic => RpcBlockRequest::Finality(Finality::Optimistic),
Reference::NearFinal => RpcBlockRequest::Finality(Finality::NearFinal),
Expand All @@ -35,41 +29,11 @@ impl RpcType for SimpleBlockRpc {
RpcBlockRequest::BlockId(BlockId::CryptoHash((*block_hash).into()))
}
};
let response = client
.block(&JsonRpcRequestForBlock {
id: "0".to_string(),
jsonrpc: "2.0".to_string(),
method: JsonRpcRequestForBlockMethod::Block,
params: request,
})
.await
.map(|r| r.into_inner())
.map_err(SendRequestError::from);

match response {
Ok(JsonRpcResponseForRpcBlockResponseAndRpcBlockError::Variant0 { result, .. }) => {
RetryResponse::Ok(result)
}
Ok(JsonRpcResponseForRpcBlockResponseAndRpcBlockError::Variant1 { error, .. }) => {
let error = SendRequestError::from(error);
to_retry_error(error, is_critical_blocks_error)
}
Err(err) => to_retry_error(err, is_critical_blocks_error),
}
}
}

impl From<ErrorWrapperForRpcBlockError> for SendRequestError<RpcBlockError> {
fn from(err: ErrorWrapperForRpcBlockError) -> Self {
match err {
ErrorWrapperForRpcBlockError::InternalError(internal_error) => {
Self::InternalError(internal_error)
}
ErrorWrapperForRpcBlockError::RequestValidationError(
rpc_request_validation_error_kind,
) => Self::RequestValidationError(rpc_request_validation_error_kind),
ErrorWrapperForRpcBlockError::HandlerError(server_error) => {
Self::ServerError(server_error)
match client.call::<_, RpcBlockResponse>("block", request).await {
Ok(response) => RetryResponse::Ok(response),
Err(err) => {
let err = SendRequestError::from(err);
to_retry_error(err, is_critical_rpc_error)
}
}
}
Expand Down
Loading
Loading