Skip to content

Commit 59fb706

Browse files
committed
feat: migrate from near-openapi-client to near-openrpc-client
Replace the OpenAPI-based RPC client dependency with the new OpenRPC-generated client. Updates all type imports, query builders, transaction handling, and error types across both the api and types crates to use the near-openrpc-client equivalents.
1 parent 37fee2c commit 59fb706

29 files changed

+937
-1211
lines changed

Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[workspace]
22
resolver = "3"
33
members = ["api", "types"]
4-
rust-version = "1.86"
54

65
[workspace.package]
76
edition = "2024"
7+
rust-version = "1.88"
88
version = "0.8.4"
99
authors = [
1010
"akorchyn <artur.yurii.korchynskyi@gmail.com>",
@@ -55,8 +55,7 @@ near-gas = { version = "0.3", features = ["serde", "borsh"] }
5555
near-token = { version = "0.3", features = ["serde", "borsh"] }
5656
near-abi = "0.4.2"
5757
near-ledger = "0.9.1"
58-
near-openapi-client = "0.7"
59-
near-openapi-types = "0.7"
58+
near-openrpc-client = { git = "https://github.com/near/near-openrpc-client-rs.git", default-features = false }
6059

6160
# Dev-dependencies
6261
near-primitives = { version = "0.34" }

api/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "near-api"
3-
rust-version = "1.85"
3+
rust-version = "1.88"
44
version.workspace = true
55
authors.workspace = true
66
license.workspace = true
@@ -27,15 +27,14 @@ openssl = { workspace = true, features = ["vendored"] }
2727
bip39 = { workspace = true, features = ["rand"] }
2828
serde = { workspace = true, features = ["derive"] }
2929
serde_json.workspace = true
30-
serde_dbgfmt.workspace = true
3130
slipped10.workspace = true
3231
url.workspace = true
3332
tokio = { workspace = true, default-features = false, features = ["time"] }
3433
tracing.workspace = true
3534
thiserror.workspace = true
3635

3736
near-ledger = { workspace = true, optional = true }
38-
near-openapi-client.workspace = true
37+
near-openrpc-client.workspace = true
3938
near-api-types.workspace = true
4039

4140
zstd.workspace = true

api/src/account/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl Account {
105105
crate::stake::Delegation(self.0.clone())
106106
}
107107

108-
/// Prepares a query to fetch the [Data](crate::Data)<[AccountView](near_api_types::AccountView)> with the account information for the given account ID.
108+
/// Prepares a query to fetch the [Data](crate::Data)<[AccountView](near_api_types::Account)> with the account information for the given account ID.
109109
///
110110
/// ## Example
111111
/// ```rust,no_run

api/src/chain.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use near_api_types::{BlockHeight, CryptoHash, Reference};
22

33
use crate::{
44
advanced::block_rpc::SimpleBlockRpc,
5-
common::query::{PostprocessHandler, RequestBuilder, RpcBlockHandler},
5+
common::query::{AndThenHandler, PostprocessHandler, RequestBuilder, RpcBlockHandler},
66
};
77

88
/// Chain-related interactions with the NEAR Protocol
@@ -79,9 +79,9 @@ impl Chain {
7979
/// # Ok(())
8080
/// # }
8181
/// ```
82-
pub fn block_hash() -> RequestBuilder<PostprocessHandler<CryptoHash, RpcBlockHandler>> {
82+
pub fn block_hash() -> RequestBuilder<AndThenHandler<CryptoHash, RpcBlockHandler>> {
8383
RequestBuilder::new(SimpleBlockRpc, Reference::Optimistic, RpcBlockHandler)
84-
.map(|data| CryptoHash::from(data.header.hash))
84+
.and_then(|data| Ok(CryptoHash::try_from(data.header.hash)?))
8585
}
8686

8787
/// Set ups a query to fetch the [RpcBlockResponse][near_api_types::RpcBlockResponse]

api/src/common/query/block_rpc.rs

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
use near_api_types::Reference;
2-
use near_openapi_client::Client;
3-
use near_openapi_client::types::{
4-
BlockId, ErrorWrapperForRpcBlockError, Finality, JsonRpcRequestForBlock,
5-
JsonRpcRequestForBlockMethod, JsonRpcResponseForRpcBlockResponseAndRpcBlockError,
6-
RpcBlockError, RpcBlockRequest, RpcBlockResponse,
7-
};
2+
use near_openrpc_client::{BlockId, Finality, RpcBlockRequest, RpcBlockResponse};
83

94
use crate::common::utils::to_retry_error;
105
use crate::{
116
NetworkConfig, advanced::RpcType, common::utils::is_critical_blocks_error,
12-
config::RetryResponse, errors::SendRequestError,
7+
config::RetryResponse, errors::SendRequestError, rpc_client::RpcClient,
138
};
149

1510
#[derive(Clone, Debug)]
@@ -19,13 +14,12 @@ pub struct SimpleBlockRpc;
1914
impl RpcType for SimpleBlockRpc {
2015
type RpcReference = Reference;
2116
type Response = RpcBlockResponse;
22-
type Error = RpcBlockError;
2317
async fn send_query(
2418
&self,
25-
client: &Client,
19+
client: &RpcClient,
2620
_network: &NetworkConfig,
2721
reference: &Reference,
28-
) -> RetryResponse<RpcBlockResponse, SendRequestError<RpcBlockError>> {
22+
) -> RetryResponse<RpcBlockResponse, SendRequestError> {
2923
let request = match reference {
3024
Reference::Optimistic => RpcBlockRequest::Finality(Finality::Optimistic),
3125
Reference::NearFinal => RpcBlockRequest::Finality(Finality::NearFinal),
@@ -35,41 +29,11 @@ impl RpcType for SimpleBlockRpc {
3529
RpcBlockRequest::BlockId(BlockId::CryptoHash((*block_hash).into()))
3630
}
3731
};
38-
let response = client
39-
.block(&JsonRpcRequestForBlock {
40-
id: "0".to_string(),
41-
jsonrpc: "2.0".to_string(),
42-
method: JsonRpcRequestForBlockMethod::Block,
43-
params: request,
44-
})
45-
.await
46-
.map(|r| r.into_inner())
47-
.map_err(SendRequestError::from);
48-
49-
match response {
50-
Ok(JsonRpcResponseForRpcBlockResponseAndRpcBlockError::Variant0 { result, .. }) => {
51-
RetryResponse::Ok(result)
52-
}
53-
Ok(JsonRpcResponseForRpcBlockResponseAndRpcBlockError::Variant1 { error, .. }) => {
54-
let error = SendRequestError::from(error);
55-
to_retry_error(error, is_critical_blocks_error)
56-
}
57-
Err(err) => to_retry_error(err, is_critical_blocks_error),
58-
}
59-
}
60-
}
61-
62-
impl From<ErrorWrapperForRpcBlockError> for SendRequestError<RpcBlockError> {
63-
fn from(err: ErrorWrapperForRpcBlockError) -> Self {
64-
match err {
65-
ErrorWrapperForRpcBlockError::InternalError(internal_error) => {
66-
Self::InternalError(internal_error)
67-
}
68-
ErrorWrapperForRpcBlockError::RequestValidationError(
69-
rpc_request_validation_error_kind,
70-
) => Self::RequestValidationError(rpc_request_validation_error_kind),
71-
ErrorWrapperForRpcBlockError::HandlerError(server_error) => {
72-
Self::ServerError(server_error)
32+
match client.call::<_, RpcBlockResponse>("block", request).await {
33+
Ok(response) => RetryResponse::Ok(response),
34+
Err(err) => {
35+
let err = SendRequestError::from(err);
36+
to_retry_error(err, is_critical_blocks_error)
7337
}
7438
}
7539
}

0 commit comments

Comments
 (0)