@@ -24,144 +24,242 @@ use crate::{provider::ProviderImplError, Provider, ProviderError};
24
24
mod transports;
25
25
pub use transports:: { HttpTransport , HttpTransportError , JsonRpcTransport } ;
26
26
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`].
27
31
#[ derive( Debug ) ]
28
32
pub struct JsonRpcClient < T > {
29
33
transport : T ,
30
34
}
31
35
36
+ /// All JSON-RPC methods as listed by the official specification.
32
37
#[ derive( Debug , Clone , Copy , Serialize , Deserialize ) ]
33
38
pub enum JsonRpcMethod {
39
+ /// The `starknet_specVersion` method.
34
40
#[ serde( rename = "starknet_specVersion" ) ]
35
41
SpecVersion ,
42
+ /// The `starknet_getBlockWithTxHashes` method.
36
43
#[ serde( rename = "starknet_getBlockWithTxHashes" ) ]
37
44
GetBlockWithTxHashes ,
45
+ /// The `starknet_getBlockWithTxs` method.
38
46
#[ serde( rename = "starknet_getBlockWithTxs" ) ]
39
47
GetBlockWithTxs ,
48
+ /// The `starknet_getBlockWithReceipts` method.
40
49
#[ serde( rename = "starknet_getBlockWithReceipts" ) ]
41
50
GetBlockWithReceipts ,
51
+ /// The `starknet_getStateUpdate` method.
42
52
#[ serde( rename = "starknet_getStateUpdate" ) ]
43
53
GetStateUpdate ,
54
+ /// The `starknet_getStorageAt` method.
44
55
#[ serde( rename = "starknet_getStorageAt" ) ]
45
56
GetStorageAt ,
57
+ /// The `starknet_getTransactionStatus` method.
46
58
#[ serde( rename = "starknet_getTransactionStatus" ) ]
47
59
GetTransactionStatus ,
60
+ /// The `starknet_getTransactionByHash` method.
48
61
#[ serde( rename = "starknet_getTransactionByHash" ) ]
49
62
GetTransactionByHash ,
63
+ /// The `starknet_getTransactionByBlockIdAndIndex` method.
50
64
#[ serde( rename = "starknet_getTransactionByBlockIdAndIndex" ) ]
51
65
GetTransactionByBlockIdAndIndex ,
66
+ /// The `starknet_getTransactionReceipt` method.
52
67
#[ serde( rename = "starknet_getTransactionReceipt" ) ]
53
68
GetTransactionReceipt ,
69
+ /// The `starknet_getClass` method.
54
70
#[ serde( rename = "starknet_getClass" ) ]
55
71
GetClass ,
72
+ /// The `starknet_getClassHashAt` method.
56
73
#[ serde( rename = "starknet_getClassHashAt" ) ]
57
74
GetClassHashAt ,
75
+ /// The `starknet_getClassAt` method.
58
76
#[ serde( rename = "starknet_getClassAt" ) ]
59
77
GetClassAt ,
78
+ /// The `starknet_getBlockTransactionCount` method.
60
79
#[ serde( rename = "starknet_getBlockTransactionCount" ) ]
61
80
GetBlockTransactionCount ,
81
+ /// The `starknet_call` method.
62
82
#[ serde( rename = "starknet_call" ) ]
63
83
Call ,
84
+ /// The `starknet_estimateFee` method.
64
85
#[ serde( rename = "starknet_estimateFee" ) ]
65
86
EstimateFee ,
87
+ /// The `starknet_estimateMessageFee` method.
66
88
#[ serde( rename = "starknet_estimateMessageFee" ) ]
67
89
EstimateMessageFee ,
90
+ /// The `starknet_blockNumber` method.
68
91
#[ serde( rename = "starknet_blockNumber" ) ]
69
92
BlockNumber ,
93
+ /// The `starknet_blockHashAndNumber` method.
70
94
#[ serde( rename = "starknet_blockHashAndNumber" ) ]
71
95
BlockHashAndNumber ,
96
+ /// The `starknet_chainId` method.
72
97
#[ serde( rename = "starknet_chainId" ) ]
73
98
ChainId ,
99
+ /// The `starknet_syncing` method.
74
100
#[ serde( rename = "starknet_syncing" ) ]
75
101
Syncing ,
102
+ /// The `starknet_getEvents` method.
76
103
#[ serde( rename = "starknet_getEvents" ) ]
77
104
GetEvents ,
105
+ /// The `starknet_getNonce` method.
78
106
#[ serde( rename = "starknet_getNonce" ) ]
79
107
GetNonce ,
108
+ /// The `starknet_addInvokeTransaction` method.
80
109
#[ serde( rename = "starknet_addInvokeTransaction" ) ]
81
110
AddInvokeTransaction ,
111
+ /// The `starknet_addDeclareTransaction` method.
82
112
#[ serde( rename = "starknet_addDeclareTransaction" ) ]
83
113
AddDeclareTransaction ,
114
+ /// The `starknet_addDeployAccountTransaction` method.
84
115
#[ serde( rename = "starknet_addDeployAccountTransaction" ) ]
85
116
AddDeployAccountTransaction ,
117
+ /// The `starknet_traceTransaction` method.
86
118
#[ serde( rename = "starknet_traceTransaction" ) ]
87
119
TraceTransaction ,
120
+ /// The `starknet_simulateTransactions` method.
88
121
#[ serde( rename = "starknet_simulateTransactions" ) ]
89
122
SimulateTransactions ,
123
+ /// The `starknet_traceBlockTransactions` method.
90
124
#[ serde( rename = "starknet_traceBlockTransactions" ) ]
91
125
TraceBlockTransactions ,
92
126
}
93
127
128
+ /// JSON-RPC request.
94
129
#[ derive( Debug , Clone ) ]
95
130
pub struct JsonRpcRequest {
131
+ /// ID of the request. Useful for identifying responses in certain transports like `WebSocket`.
96
132
pub id : u64 ,
133
+ /// Data of the requeest.
97
134
pub data : JsonRpcRequestData ,
98
135
}
99
136
137
+ /// Typed request data for Starknet JSON-RPC requests.
100
138
#[ derive( Debug , Clone ) ]
101
139
pub enum JsonRpcRequestData {
140
+ /// Request data for `starknet_specVersion`.
102
141
SpecVersion ( SpecVersionRequest ) ,
142
+ /// Request data for `starknet_getBlockWithTxHashes`.
103
143
GetBlockWithTxHashes ( GetBlockWithTxHashesRequest ) ,
144
+ /// Request data for `starknet_getBlockWithTxs`.
104
145
GetBlockWithTxs ( GetBlockWithTxsRequest ) ,
146
+ /// Request data for `starknet_getBlockWithReceipts`.
105
147
GetBlockWithReceipts ( GetBlockWithReceiptsRequest ) ,
148
+ /// Request data for `starknet_getStateUpdate`.
106
149
GetStateUpdate ( GetStateUpdateRequest ) ,
150
+ /// Request data for `starknet_getStorageAt`.
107
151
GetStorageAt ( GetStorageAtRequest ) ,
152
+ /// Request data for `starknet_getTransactionStatus`.
108
153
GetTransactionStatus ( GetTransactionStatusRequest ) ,
154
+ /// Request data for `starknet_getTransactionByHash`.
109
155
GetTransactionByHash ( GetTransactionByHashRequest ) ,
156
+ /// Request data for `starknet_getTransactionByBlockIdAndIndex`.
110
157
GetTransactionByBlockIdAndIndex ( GetTransactionByBlockIdAndIndexRequest ) ,
158
+ /// Request data for `starknet_getTransactionReceipt`.
111
159
GetTransactionReceipt ( GetTransactionReceiptRequest ) ,
160
+ /// Request data for `starknet_getClass`.
112
161
GetClass ( GetClassRequest ) ,
162
+ /// Request data for `starknet_getClassHashAt`.
113
163
GetClassHashAt ( GetClassHashAtRequest ) ,
164
+ /// Request data for `starknet_getClassAt`.
114
165
GetClassAt ( GetClassAtRequest ) ,
166
+ /// Request data for `starknet_getBlockTransactionCount`.
115
167
GetBlockTransactionCount ( GetBlockTransactionCountRequest ) ,
168
+ /// Request data for `starknet_call`.
116
169
Call ( CallRequest ) ,
170
+ /// Request data for `starknet_estimateFee`.
117
171
EstimateFee ( EstimateFeeRequest ) ,
172
+ /// Request data for `starknet_estimateMessageFee`.
118
173
EstimateMessageFee ( EstimateMessageFeeRequest ) ,
174
+ /// Request data for `starknet_blockNumber`.
119
175
BlockNumber ( BlockNumberRequest ) ,
176
+ /// Request data for `starknet_blockHashAndNumber`.
120
177
BlockHashAndNumber ( BlockHashAndNumberRequest ) ,
178
+ /// Request data for `starknet_chainId`.
121
179
ChainId ( ChainIdRequest ) ,
180
+ /// Request data for `starknet_syncing`.
122
181
Syncing ( SyncingRequest ) ,
182
+ /// Request data for `starknet_getEvents`.
123
183
GetEvents ( GetEventsRequest ) ,
184
+ /// Request data for `starknet_getNonce`.
124
185
GetNonce ( GetNonceRequest ) ,
186
+ /// Request data for `starknet_addInvokeTransaction`.
125
187
AddInvokeTransaction ( AddInvokeTransactionRequest ) ,
188
+ /// Request data for `starknet_addDeclareTransaction`.
126
189
AddDeclareTransaction ( AddDeclareTransactionRequest ) ,
190
+ /// Request data for `starknet_addDeployAccountTransaction`.
127
191
AddDeployAccountTransaction ( AddDeployAccountTransactionRequest ) ,
192
+ /// Request data for `starknet_traceTransaction`.
128
193
TraceTransaction ( TraceTransactionRequest ) ,
194
+ /// Request data for `starknet_simulateTransactions`.
129
195
SimulateTransactions ( SimulateTransactionsRequest ) ,
196
+ /// Request data for `starknet_traceBlockTransactions`.
130
197
TraceBlockTransactions ( TraceBlockTransactionsRequest ) ,
131
198
}
132
199
200
+ /// Errors from JSON-RPC client.
133
201
#[ derive( Debug , thiserror:: Error ) ]
134
202
pub enum JsonRpcClientError < T > {
203
+ /// JSON serialization/deserialization erors.
135
204
#[ error( transparent) ]
136
205
JsonError ( serde_json:: Error ) ,
206
+ /// Transport-specific errors.
137
207
#[ error( transparent) ]
138
208
TransportError ( T ) ,
209
+ /// An unsuccessful response returned from the server is encountered.
139
210
#[ error( transparent) ]
140
211
JsonRpcError ( JsonRpcError ) ,
141
212
}
142
213
214
+ /// An unsuccessful response returned from the server.
143
215
#[ derive( Debug , Deserialize ) ]
144
216
pub struct JsonRpcError {
217
+ /// Error code.
145
218
pub code : i64 ,
219
+ /// Error message.
146
220
pub message : String ,
221
+ /// Additional error data if any.
147
222
#[ serde( skip_serializing_if = "Option::is_none" ) ]
148
223
pub data : Option < serde_json:: Value > ,
149
224
}
150
225
226
+ /// JSON-RPC response returned from a server.
151
227
#[ derive( Debug , Deserialize ) ]
152
228
#[ serde( untagged) ]
153
229
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
+ } ,
156
244
}
157
245
158
246
/// 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.
159
254
#[ derive( Debug , thiserror:: Error ) ]
160
255
pub enum JsonRpcErrorConversionError {
256
+ /// The error code is outside of the range specified by the specification.
161
257
#[ error( "unknown error code" ) ]
162
258
UnknownCode ,
259
+ /// Error data is expected but missing.
163
260
#[ error( "missing data field" ) ]
164
261
MissingData ,
262
+ /// Error data is malformed.
165
263
#[ error( "unable to parse the data field" ) ]
166
264
DataParsingFailure ,
167
265
}
@@ -175,6 +273,7 @@ struct Felt(#[serde_as(as = "UfeHex")] pub FeltPrimitive);
175
273
struct FeltArray ( #[ serde_as( as = "Vec<UfeHex>" ) ] pub Vec < FeltPrimitive > ) ;
176
274
177
275
impl < T > JsonRpcClient < T > {
276
+ /// Constructs a new [`JsonRpcClient`] from a transport.
178
277
pub const fn new ( transport : T ) -> Self {
179
278
Self { transport }
180
279
}
0 commit comments