Skip to content

Commit a723596

Browse files
committed
jsonrpc: public interfaces are documented
1 parent 0793146 commit a723596

File tree

5 files changed

+112
-10
lines changed

5 files changed

+112
-10
lines changed

example/client/jsonrpc/cpp20.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,19 @@ co_main(
9595
<< std::setprecision(3) << dec_float(to_int(gas_price)) / dec_float("1e10")
9696
<< " GWEI\n";
9797

98+
// Get ETH/USD price from a Chainlink oracle
99+
auto eth_usd = co_await client[eth_call](
100+
{
101+
{
102+
{"to", "0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419"}, // ETH/USD price feed
103+
{"data", "0x50d25bcd"} // latestRoundData() selector
104+
},
105+
"latest"
106+
});
107+
std::cout
108+
<< "ETH/USD: "
109+
<< std::fixed << dec_float(to_int(eth_usd)) / dec_float("1e8") << '\n';
110+
98111
// Gracefully close the stream
99112
auto [ec] = co_await client.async_shutdown(asio::as_tuple);
100113
if(ec && ec != asio::ssl::error::stream_truncated)
@@ -105,14 +118,15 @@ co_main(
105118
Sample output:
106119
107120
web3 client: "erigon/3.0.4/linux-amd64/go1.23.9"
108-
Block number: 23175159
109-
Block hash: "0xd476f0a0dd29be4d63bc737937f6c40b024906f1a864ee38aa1a03f91ce55efd"
110-
Block size: 87166 Bytes
111-
Timestamp: 1755606983
112-
Transactions: 176
121+
Block number: 23189485
122+
Block hash: "0xc42ff6625921df3a09495cbea3353ab6e60167319d2c64cedfdd265b7af0a738"
123+
Block size: 132074 Bytes
124+
Timestamp: 1755779639
125+
Transactions: 259
113126
Balance: 180774.35501503312 ETH
114127
Gas estimate: 21000
115-
Gas price: 0.0337 GWEI
128+
Gas price: 0.039 GWEI
129+
ETH/USD: 4280.746
116130
*/
117131

118132
int

example/client/jsonrpc/jsonrpc/any_stream.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace jsonrpc {
1919

20+
/// Interface for a stream that can be used with @ref client.
2021
class any_stream
2122
{
2223
public:

example/client/jsonrpc/jsonrpc/client.hpp

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
namespace jsonrpc {
2828

29+
/// A JSON-RPC 2.0 client.
2930
class client
3031
{
3132
std::unique_ptr<any_stream> stream_;
@@ -47,49 +48,70 @@ class client
4748
using next_layer_type = any_stream;
4849

4950
/// The type of the executor associated with the object.
50-
using executor_type = typename next_layer_type::executor_type;
51+
using executor_type = typename any_stream::executor_type;
5152

53+
/** Constructor.
54+
55+
Constructs a client capable of connecting to
56+
an HTTP or HTTPS endpoint.
57+
*/
5258
client(
5359
boost::urls::url endpoint,
5460
const boost::rts::context& rts_ctx,
5561
boost::asio::any_io_executor exec,
5662
boost::asio::ssl::context& ssl_ctx);
5763

64+
/** Constructor.
65+
66+
Constructs a client using the supplied
67+
@ref any_stream instance.
68+
*/
5869
client(
5970
boost::urls::url endpoint,
6071
const boost::rts::context& rts_ctx,
6172
std::unique_ptr<any_stream> stream);
6273

74+
/// Get the executor associated with the object.
6375
executor_type
6476
get_executor() noexcept
6577
{
6678
return stream_->get_executor();
6779
}
6880

81+
/// Get a reference to the next layer.
6982
next_layer_type&
7083
next_layer() noexcept
7184
{
7285
return *stream_;
7386
}
7487

88+
/// Get a reference to the next layer.
7589
next_layer_type const&
7690
next_layer() const noexcept
7791
{
7892
return *stream_;
7993
}
8094

95+
/// Return the endpoint that the client is configured with.
8196
boost::urls::url_view
8297
endpoint() const noexcept
8398
{
8499
return endpoint_;
85100
}
86101

102+
/** Return a reference to the fields container of the HTTP
103+
request message.
104+
105+
This function can be used to customize HTTP headers, for
106+
example, to add the required credentials.
107+
*/
87108
boost::http_proto::fields_base&
88109
http_fields()
89110
{
90111
return req_;
91112
}
92113

114+
/// Connect to the endpoint.
93115
template<
94116
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code))
95117
CompletionToken = boost::asio::deferred_t>
@@ -103,6 +125,7 @@ class client
103125
this);
104126
}
105127

128+
/// Shutdown the stream.
106129
template<
107130
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(boost::system::error_code))
108131
CompletionToken = boost::asio::deferred_t>
@@ -119,7 +142,7 @@ class client
119142
template<typename Signature>
120143
class invoker;
121144

122-
/** Calls a remote procedure that takes no parameters.
145+
/** Call a remote procedure that takes no parameters.
123146
124147
Instances of this type are returned from @ref operator[].
125148
@@ -131,6 +154,14 @@ class client
131154
using invoker_base::invoker_base;
132155
public:
133156

157+
/** Call a remote procedure that takes no parameters.
158+
159+
The result type depends on the signature of the @ref method object
160+
used to instantiate the invoker.
161+
162+
@param token The completion token used to produce a completion
163+
handler, which will be invoked when the call completes.
164+
*/
134165
template<
135166
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error, Return))
136167
CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
@@ -145,7 +176,7 @@ class client
145176
}
146177
};
147178

148-
/** Calls a remote procedure with positional parameters (as an array).
179+
/** Call a remote procedure with positional parameters (array).
149180
150181
Instances of this type are returned from @ref operator[].
151182
@@ -157,6 +188,17 @@ class client
157188
using invoker_base::invoker_base;
158189
public:
159190

191+
/** Call a remote procedure with positional parameters (array).
192+
193+
The result type depends on the signature of the @ref method object
194+
used to instantiate the invoker.
195+
196+
@param params A JSON array containing the positional parameters to
197+
use when calling the server method.
198+
199+
@param token The completion token used to produce a completion
200+
handler, which will be invoked when the call completes.
201+
*/
160202
template<
161203
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error, Return))
162204
CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
@@ -172,7 +214,7 @@ class client
172214
}
173215
};
174216

175-
/** Calls a remote procedure with named parameters (as an object).
217+
/** Call a remote procedure with named parameters (object).
176218
177219
Instances of this type are returned from @ref operator[].
178220
@@ -184,6 +226,17 @@ class client
184226
using invoker_base::invoker_base;
185227
public:
186228

229+
/** Call a remote procedure with named parameters (object).
230+
231+
The result type depends on the signature of the @ref method object
232+
used to instantiate the invoker.
233+
234+
@param params A JSON object containing the named parameters to use
235+
when calling the server method.
236+
237+
@param token The completion token used to produce a completion
238+
handler, which will be invoked when the call completes.
239+
*/
187240
template<
188241
BOOST_ASIO_COMPLETION_TOKEN_FOR(void(error, Return))
189242
CompletionToken BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)>
@@ -199,6 +252,20 @@ class client
199252
}
200253
};
201254

255+
/** Return an @ref invoker instance that can be used to asynchronously
256+
invoke the specified method.
257+
258+
Example:
259+
@code
260+
client[method_a]({ "param1", "param2" }, completion_token);
261+
@endcode
262+
263+
@return An @ref invoker capable of asynchronously calling the specified
264+
method.
265+
266+
@param m The @ref method object containing the name and signature of the
267+
remote procedure.
268+
*/
202269
template<typename Signature>
203270
invoker<Signature>
204271
operator[](method<Signature> m) noexcept

example/client/jsonrpc/jsonrpc/error.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
namespace jsonrpc {
2020

21+
/** An error type that encapsulates both an error code
22+
and an optional JSON object containing additional error
23+
details as provided by the server.
24+
*/
2125
class error
2226
{
2327
boost::system::error_code ec_;
@@ -43,12 +47,16 @@ class error
4347
return *this;
4448
}
4549

50+
/// Return the error code
4651
const boost::system::error_code&
4752
code() const noexcept
4853
{
4954
return ec_;
5055
}
5156

57+
/** Return an optional JSON object containing additional
58+
error details as provided by the server.
59+
*/
5260
const boost::json::object&
5361
object() const noexcept
5462
{

example/client/jsonrpc/jsonrpc/method.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@
1414

1515
namespace jsonrpc {
1616

17+
/** Containin the name and signature of a remote
18+
procedure.
19+
20+
@par Example:
21+
@code
22+
method<json::string()> web3_clientVersion = "web3_clientVersion";
23+
@endcode
24+
25+
@tparam Signature The signature of the method.
26+
*/
1727
template<typename Signature>
1828
struct method;
1929

30+
/// @copydoc method
2031
template<typename Return>
2132
struct method<Return()>
2233
{
@@ -29,6 +40,7 @@ struct method<Return()>
2940
}
3041
};
3142

43+
/// @copydoc method
3244
template<typename Return, typename Param>
3345
struct method<Return(Param)>
3446
{

0 commit comments

Comments
 (0)