Skip to content

Commit d297f81

Browse files
authored
docs: add docs for starknet-providers (#639)
1 parent 4726535 commit d297f81

File tree

6 files changed

+190
-38
lines changed

6 files changed

+190
-38
lines changed

starknet-providers/src/any.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ use crate::{
2626
/// it's still needed anymore.
2727
#[derive(Debug)]
2828
pub enum AnyProvider {
29+
/// JSON-RPC provider.
2930
JsonRpcHttp(JsonRpcClient<HttpTransport>),
31+
/// Sequencer gateway provider.
3032
SequencerGateway(SequencerGatewayProvider),
3133
}
3234

starknet-providers/src/jsonrpc/mod.rs

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,144 +24,242 @@ use crate::{provider::ProviderImplError, Provider, ProviderError};
2424
mod transports;
2525
pub use transports::{HttpTransport, HttpTransportError, JsonRpcTransport};
2626

27+
/// A generic JSON-RPC client with any transport.
28+
///
29+
/// A "transport" is any implementation that can send JSON-RPC requests and receive responses. This
30+
/// most commonly happens over a network via HTTP connections, as with [`HttpTransport`].
2731
#[derive(Debug)]
2832
pub struct JsonRpcClient<T> {
2933
transport: T,
3034
}
3135

36+
/// All JSON-RPC methods as listed by the official specification.
3237
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
3338
pub enum JsonRpcMethod {
39+
/// The `starknet_specVersion` method.
3440
#[serde(rename = "starknet_specVersion")]
3541
SpecVersion,
42+
/// The `starknet_getBlockWithTxHashes` method.
3643
#[serde(rename = "starknet_getBlockWithTxHashes")]
3744
GetBlockWithTxHashes,
45+
/// The `starknet_getBlockWithTxs` method.
3846
#[serde(rename = "starknet_getBlockWithTxs")]
3947
GetBlockWithTxs,
48+
/// The `starknet_getBlockWithReceipts` method.
4049
#[serde(rename = "starknet_getBlockWithReceipts")]
4150
GetBlockWithReceipts,
51+
/// The `starknet_getStateUpdate` method.
4252
#[serde(rename = "starknet_getStateUpdate")]
4353
GetStateUpdate,
54+
/// The `starknet_getStorageAt` method.
4455
#[serde(rename = "starknet_getStorageAt")]
4556
GetStorageAt,
57+
/// The `starknet_getTransactionStatus` method.
4658
#[serde(rename = "starknet_getTransactionStatus")]
4759
GetTransactionStatus,
60+
/// The `starknet_getTransactionByHash` method.
4861
#[serde(rename = "starknet_getTransactionByHash")]
4962
GetTransactionByHash,
63+
/// The `starknet_getTransactionByBlockIdAndIndex` method.
5064
#[serde(rename = "starknet_getTransactionByBlockIdAndIndex")]
5165
GetTransactionByBlockIdAndIndex,
66+
/// The `starknet_getTransactionReceipt` method.
5267
#[serde(rename = "starknet_getTransactionReceipt")]
5368
GetTransactionReceipt,
69+
/// The `starknet_getClass` method.
5470
#[serde(rename = "starknet_getClass")]
5571
GetClass,
72+
/// The `starknet_getClassHashAt` method.
5673
#[serde(rename = "starknet_getClassHashAt")]
5774
GetClassHashAt,
75+
/// The `starknet_getClassAt` method.
5876
#[serde(rename = "starknet_getClassAt")]
5977
GetClassAt,
78+
/// The `starknet_getBlockTransactionCount` method.
6079
#[serde(rename = "starknet_getBlockTransactionCount")]
6180
GetBlockTransactionCount,
81+
/// The `starknet_call` method.
6282
#[serde(rename = "starknet_call")]
6383
Call,
84+
/// The `starknet_estimateFee` method.
6485
#[serde(rename = "starknet_estimateFee")]
6586
EstimateFee,
87+
/// The `starknet_estimateMessageFee` method.
6688
#[serde(rename = "starknet_estimateMessageFee")]
6789
EstimateMessageFee,
90+
/// The `starknet_blockNumber` method.
6891
#[serde(rename = "starknet_blockNumber")]
6992
BlockNumber,
93+
/// The `starknet_blockHashAndNumber` method.
7094
#[serde(rename = "starknet_blockHashAndNumber")]
7195
BlockHashAndNumber,
96+
/// The `starknet_chainId` method.
7297
#[serde(rename = "starknet_chainId")]
7398
ChainId,
99+
/// The `starknet_syncing` method.
74100
#[serde(rename = "starknet_syncing")]
75101
Syncing,
102+
/// The `starknet_getEvents` method.
76103
#[serde(rename = "starknet_getEvents")]
77104
GetEvents,
105+
/// The `starknet_getNonce` method.
78106
#[serde(rename = "starknet_getNonce")]
79107
GetNonce,
108+
/// The `starknet_addInvokeTransaction` method.
80109
#[serde(rename = "starknet_addInvokeTransaction")]
81110
AddInvokeTransaction,
111+
/// The `starknet_addDeclareTransaction` method.
82112
#[serde(rename = "starknet_addDeclareTransaction")]
83113
AddDeclareTransaction,
114+
/// The `starknet_addDeployAccountTransaction` method.
84115
#[serde(rename = "starknet_addDeployAccountTransaction")]
85116
AddDeployAccountTransaction,
117+
/// The `starknet_traceTransaction` method.
86118
#[serde(rename = "starknet_traceTransaction")]
87119
TraceTransaction,
120+
/// The `starknet_simulateTransactions` method.
88121
#[serde(rename = "starknet_simulateTransactions")]
89122
SimulateTransactions,
123+
/// The `starknet_traceBlockTransactions` method.
90124
#[serde(rename = "starknet_traceBlockTransactions")]
91125
TraceBlockTransactions,
92126
}
93127

128+
/// JSON-RPC request.
94129
#[derive(Debug, Clone)]
95130
pub struct JsonRpcRequest {
131+
/// ID of the request. Useful for identifying responses in certain transports like `WebSocket`.
96132
pub id: u64,
133+
/// Data of the requeest.
97134
pub data: JsonRpcRequestData,
98135
}
99136

137+
/// Typed request data for Starknet JSON-RPC requests.
100138
#[derive(Debug, Clone)]
101139
pub enum JsonRpcRequestData {
140+
/// Request data for `starknet_specVersion`.
102141
SpecVersion(SpecVersionRequest),
142+
/// Request data for `starknet_getBlockWithTxHashes`.
103143
GetBlockWithTxHashes(GetBlockWithTxHashesRequest),
144+
/// Request data for `starknet_getBlockWithTxs`.
104145
GetBlockWithTxs(GetBlockWithTxsRequest),
146+
/// Request data for `starknet_getBlockWithReceipts`.
105147
GetBlockWithReceipts(GetBlockWithReceiptsRequest),
148+
/// Request data for `starknet_getStateUpdate`.
106149
GetStateUpdate(GetStateUpdateRequest),
150+
/// Request data for `starknet_getStorageAt`.
107151
GetStorageAt(GetStorageAtRequest),
152+
/// Request data for `starknet_getTransactionStatus`.
108153
GetTransactionStatus(GetTransactionStatusRequest),
154+
/// Request data for `starknet_getTransactionByHash`.
109155
GetTransactionByHash(GetTransactionByHashRequest),
156+
/// Request data for `starknet_getTransactionByBlockIdAndIndex`.
110157
GetTransactionByBlockIdAndIndex(GetTransactionByBlockIdAndIndexRequest),
158+
/// Request data for `starknet_getTransactionReceipt`.
111159
GetTransactionReceipt(GetTransactionReceiptRequest),
160+
/// Request data for `starknet_getClass`.
112161
GetClass(GetClassRequest),
162+
/// Request data for `starknet_getClassHashAt`.
113163
GetClassHashAt(GetClassHashAtRequest),
164+
/// Request data for `starknet_getClassAt`.
114165
GetClassAt(GetClassAtRequest),
166+
/// Request data for `starknet_getBlockTransactionCount`.
115167
GetBlockTransactionCount(GetBlockTransactionCountRequest),
168+
/// Request data for `starknet_call`.
116169
Call(CallRequest),
170+
/// Request data for `starknet_estimateFee`.
117171
EstimateFee(EstimateFeeRequest),
172+
/// Request data for `starknet_estimateMessageFee`.
118173
EstimateMessageFee(EstimateMessageFeeRequest),
174+
/// Request data for `starknet_blockNumber`.
119175
BlockNumber(BlockNumberRequest),
176+
/// Request data for `starknet_blockHashAndNumber`.
120177
BlockHashAndNumber(BlockHashAndNumberRequest),
178+
/// Request data for `starknet_chainId`.
121179
ChainId(ChainIdRequest),
180+
/// Request data for `starknet_syncing`.
122181
Syncing(SyncingRequest),
182+
/// Request data for `starknet_getEvents`.
123183
GetEvents(GetEventsRequest),
184+
/// Request data for `starknet_getNonce`.
124185
GetNonce(GetNonceRequest),
186+
/// Request data for `starknet_addInvokeTransaction`.
125187
AddInvokeTransaction(AddInvokeTransactionRequest),
188+
/// Request data for `starknet_addDeclareTransaction`.
126189
AddDeclareTransaction(AddDeclareTransactionRequest),
190+
/// Request data for `starknet_addDeployAccountTransaction`.
127191
AddDeployAccountTransaction(AddDeployAccountTransactionRequest),
192+
/// Request data for `starknet_traceTransaction`.
128193
TraceTransaction(TraceTransactionRequest),
194+
/// Request data for `starknet_simulateTransactions`.
129195
SimulateTransactions(SimulateTransactionsRequest),
196+
/// Request data for `starknet_traceBlockTransactions`.
130197
TraceBlockTransactions(TraceBlockTransactionsRequest),
131198
}
132199

200+
/// Errors from JSON-RPC client.
133201
#[derive(Debug, thiserror::Error)]
134202
pub enum JsonRpcClientError<T> {
203+
/// JSON serialization/deserialization erors.
135204
#[error(transparent)]
136205
JsonError(serde_json::Error),
206+
/// Transport-specific errors.
137207
#[error(transparent)]
138208
TransportError(T),
209+
/// An unsuccessful response returned from the server is encountered.
139210
#[error(transparent)]
140211
JsonRpcError(JsonRpcError),
141212
}
142213

214+
/// An unsuccessful response returned from the server.
143215
#[derive(Debug, Deserialize)]
144216
pub struct JsonRpcError {
217+
/// Error code.
145218
pub code: i64,
219+
/// Error message.
146220
pub message: String,
221+
/// Additional error data if any.
147222
#[serde(skip_serializing_if = "Option::is_none")]
148223
pub data: Option<serde_json::Value>,
149224
}
150225

226+
/// JSON-RPC response returned from a server.
151227
#[derive(Debug, Deserialize)]
152228
#[serde(untagged)]
153229
pub enum JsonRpcResponse<T> {
154-
Success { id: u64, result: T },
155-
Error { id: u64, error: JsonRpcError },
230+
/// Successful response.
231+
Success {
232+
/// Same ID as the corresponding request.
233+
id: u64,
234+
/// Response data.
235+
result: T,
236+
},
237+
/// Unsuccessful response.
238+
Error {
239+
/// Same ID as the corresponding request.
240+
id: u64,
241+
/// Error details.
242+
error: JsonRpcError,
243+
},
156244
}
157245

158246
/// Failures trying to parse a [`JsonRpcError`] into [`StarknetError`].
247+
///
248+
/// [`StarknetError`] is the standard, provider-agnostic error type that all [`Provider`]
249+
/// implementations should strive to return in an error case, in a best-effort basis. This allows
250+
/// for unified error handling logic.
251+
///
252+
/// However, not all error cases can be properly converted, and this error type represents the cases
253+
/// when such failure happens.
159254
#[derive(Debug, thiserror::Error)]
160255
pub enum JsonRpcErrorConversionError {
256+
/// The error code is outside of the range specified by the specification.
161257
#[error("unknown error code")]
162258
UnknownCode,
259+
/// Error data is expected but missing.
163260
#[error("missing data field")]
164261
MissingData,
262+
/// Error data is malformed.
165263
#[error("unable to parse the data field")]
166264
DataParsingFailure,
167265
}
@@ -175,6 +273,7 @@ struct Felt(#[serde_as(as = "UfeHex")] pub FeltPrimitive);
175273
struct FeltArray(#[serde_as(as = "Vec<UfeHex>")] pub Vec<FeltPrimitive>);
176274

177275
impl<T> JsonRpcClient<T> {
276+
/// Constructs a new [`JsonRpcClient`] from a transport.
178277
pub const fn new(transport: T) -> Self {
179278
Self { transport }
180279
}

starknet-providers/src/jsonrpc/transports/http.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@ use serde::{de::DeserializeOwned, Serialize};
55

66
use crate::jsonrpc::{transports::JsonRpcTransport, JsonRpcMethod, JsonRpcResponse};
77

8+
/// A [`JsonRpcTransport`] implementation that uses HTTP connections.
89
#[derive(Debug)]
910
pub struct HttpTransport {
1011
client: Client,
1112
url: Url,
1213
headers: Vec<(String, String)>,
1314
}
1415

16+
/// Errors using [`HttpTransport`].
1517
#[derive(Debug, thiserror::Error)]
1618
#[error(transparent)]
1719
pub enum HttpTransportError {
20+
/// HTTP-related errors.
1821
Reqwest(reqwest::Error),
22+
/// JSON serialization/deserialization errors.
1923
Json(serde_json::Error),
2024
}
2125

@@ -28,10 +32,15 @@ struct JsonRpcRequest<T> {
2832
}
2933

3034
impl HttpTransport {
35+
/// Constructs [`HttpTransport`] from a JSON-RPC server URL, using default HTTP client settings.
36+
///
37+
/// To use custom HTTP settings (e.g. proxy, timeout), use
38+
/// [`new_with_client`](fn.new_with_client) instead.
3139
pub fn new(url: impl Into<Url>) -> Self {
3240
Self::new_with_client(url, Client::new())
3341
}
3442

43+
/// Constructs [`HttpTransport`] from a JSON-RPC server URL and a custom `reqwest` client.
3544
pub fn new_with_client(url: impl Into<Url>, client: Client) -> Self {
3645
Self {
3746
client,

starknet-providers/src/jsonrpc/transports/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ use crate::jsonrpc::{JsonRpcMethod, JsonRpcResponse};
88
mod http;
99
pub use http::{HttpTransport, HttpTransportError};
1010

11+
/// Any type that is capable of producing JSON-RPC responses when given JSON-RPC requests. An
12+
/// implementation does not necessarily use the network, but typically does.
1113
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
1214
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
1315
#[auto_impl(&, Box, Arc)]
1416
pub trait JsonRpcTransport {
17+
/// Possible errors processing requests.
1518
type Error: Error + Send + Sync;
1619

20+
/// Sends a JSON-RPC request to retrieve a response.
1721
async fn send_request<P, R>(
1822
&self,
1923
method: JsonRpcMethod,

starknet-providers/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
#![doc = include_str!("../README.md")]
1+
//! Clients for interacting with Starknet nodes and sequencers.
2+
//!
3+
//! This crate provides the [`Provider`] trait for abstraction over means of accessing the Starknet
4+
//! network. The most commonly used implementation is [`JsonRpcClient`] with
5+
//! [`HttpTransport`](jsonrpc::HttpTransport).
6+
7+
#![deny(missing_docs)]
28

39
mod provider;
410
pub use provider::{Provider, ProviderError};
511

12+
// Sequencer-related functionalities are mostly deprecated so we skip the docs.
13+
/// Module containing types related to the (now deprecated) sequencer gateway client.
14+
#[allow(missing_docs)]
615
pub mod sequencer;
716
pub use sequencer::{
817
GatewayClientError as SequencerGatewayProviderError, SequencerGatewayProvider,
918
};
1019

20+
/// Module containing types related to JSON-RPC clients and servers.
1121
pub mod jsonrpc;
1222
pub use jsonrpc::JsonRpcClient;
1323

0 commit comments

Comments
 (0)