Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions example/client/get/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class session

// Enable compression
#ifdef BOOST_RTS_HAS_BROTLI
req_.set(field::accept_encoding, "br");
req_.append(field::accept_encoding, "br");
#endif
#ifdef BOOST_RTS_HAS_ZLIB
req_.set(field::accept_encoding, "deflate, gzip");
req_.append(field::accept_encoding, "deflate, gzip");
#endif

// Keep original URL for possible redirect
Expand Down
22 changes: 18 additions & 4 deletions example/client/jsonrpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,32 @@ if (WIN32)
target_link_libraries(http_io_example_client_jsonrpc_lib PUBLIC crypt32)
endif()

# CPP11 Example
add_executable(http_io_example_client_jsonrpc_cpp11 cpp11.cpp eth_methods.hpp Jamfile)
set_property(TARGET http_io_example_client_jsonrpc_cpp11
PROPERTY FOLDER "examples")
target_link_libraries(http_io_example_client_jsonrpc_cpp11
PRIVATE
http_io_example_client_jsonrpc_lib)
set_property(TARGET http_io_example_client_jsonrpc_cpp11
PROPERTY FOLDER "examples")
if (TARGET boost_rts_zlib)
target_link_libraries(http_io_example_client_jsonrpc_cpp11 PRIVATE boost_rts_zlib)
endif()
if (TARGET boost_rts_brotli)
target_link_libraries(http_io_example_client_jsonrpc_cpp11 PRIVATE boost_rts_brotli)
endif()

# CPP20 Example
if (CMAKE_CXX_STANDARD EQUAL 20)
add_executable(http_io_example_client_jsonrpc_cpp20 cpp20.cpp eth_methods.hpp Jamfile)
set_property(TARGET http_io_example_client_jsonrpc_cpp20
PROPERTY FOLDER "examples")
target_link_libraries(http_io_example_client_jsonrpc_cpp20
PRIVATE
http_io_example_client_jsonrpc_lib)
set_property(TARGET http_io_example_client_jsonrpc_cpp20
PROPERTY FOLDER "examples")
if (TARGET boost_rts_zlib)
target_link_libraries(http_io_example_client_jsonrpc_cpp20 PRIVATE boost_rts_zlib)
endif()
if (TARGET boost_rts_brotli)
target_link_libraries(http_io_example_client_jsonrpc_cpp20 PRIVATE boost_rts_brotli)
endif()
endif ()
42 changes: 29 additions & 13 deletions example/client/jsonrpc/cpp11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
#include "eth_methods.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/rts/brotli/decode.hpp>
#include <boost/rts/context.hpp>
#include <boost/rts/zlib/inflate.hpp>

#include <functional>
#include <iostream>
Expand Down Expand Up @@ -60,7 +62,8 @@ class session
return fail(ec, "connect");

// Get Ethereum node client software and version
client_[web3_clientVersion](
client_.async_call(
web3_clientVersion,
std::bind(&session::on_clientVersion, this, _1, _2));
}

Expand All @@ -76,7 +79,8 @@ class session
<< version << '\n';

// Get the latest block number
client_[eth_blockNumber](
client_.async_call(
eth_blockNumber,
std::bind(&session::on_blockNumber, this, _1, _2));
}

Expand All @@ -90,9 +94,10 @@ class session
std::cout
<< "Block height: "
<< block_num << '\n';

// Get block details
client_[eth_getBlockByNumber](
client_.async_call(
eth_getBlockByNumber,
{ block_num, false },
std::bind(&session::on_getBlockByNumber, this, _1, _2));
}
Expand All @@ -110,7 +115,8 @@ class session
std::cout<< "Transactions: " << block["transactions"].as_array().size() << '\n';

// Get account balance
client_[eth_getBalance](
client_.async_call(
eth_getBalance,
{
"0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
block["number"]
Expand All @@ -128,9 +134,10 @@ class session
std::cout
<< "Balance: "
<< balance << '\n';

// Estimate gas for a transfer
client_[eth_estimateGas](
client_.async_call(
eth_estimateGas,
{
json::object
{
Expand All @@ -152,9 +159,10 @@ class session
std::cout
<< "Gas estimate: "
<< gas_estimate << '\n';

// Get the current gas price
client_[eth_gasPrice](
client_.async_call(
eth_gasPrice,
std::bind(&session::on_gasPrice, this, _1, _2));
}

Expand Down Expand Up @@ -194,8 +202,8 @@ class session
void
fail(const jsonrpc::error& error)
{
if(!error.object().empty())
std::cerr << error.object() << " ";
if(!error.info().empty())
std::cerr << error.info() << " ";
std::cerr << error.code().what() << "\n";
}
};
Expand Down Expand Up @@ -233,12 +241,20 @@ main(int, char*[])
{
http_proto::response_parser::config cfg;
cfg.min_buffer = 64 * 1024;
#ifdef BOOST_RTS_HAS_BROTLI
cfg.apply_brotli_decoder = true;
rts::brotli::install_decode_service(rts_ctx);
#endif
#ifdef BOOST_RTS_HAS_ZLIB
cfg.apply_deflate_decoder = true;
cfg.apply_gzip_decoder = true;
rts::zlib::install_inflate_service(rts_ctx);
#endif
http_proto::install_parser_service(rts_ctx, cfg);
}

// Install serializer service with default configuration
http_proto::serializer::config cfg;
http_proto::install_serializer_service(rts_ctx, cfg);
http_proto::install_serializer_service(rts_ctx, {});

// Root certificates used for verification
ssl_ctx.set_default_verify_paths();
Expand Down
31 changes: 22 additions & 9 deletions example/client/jsonrpc/cpp20.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
#include <boost/asio/io_context.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/rts/brotli/decode.hpp>
#include <boost/rts/context.hpp>
#include <boost/rts/zlib/inflate.hpp>

#include <iostream>

Expand Down Expand Up @@ -48,16 +50,16 @@ co_main(
// Get Ethereum node client software and version
std::cout
<< "web3 client: "
<< co_await client[web3_clientVersion]() << '\n';
<< co_await client.async_call(web3_clientVersion) << '\n';

// Get the latest block number
auto block_num = co_await client[eth_blockNumber]();
auto block_num = co_await client.async_call(eth_blockNumber);
std::cout
<< "Block number: "
<< to_int(block_num) << '\n';

// Get block details
auto block = co_await client[eth_getBlockByNumber]({ block_num, false });
auto block = co_await client.async_call(eth_getBlockByNumber, { block_num, false });
std::cout<< "Block hash: " << block["hash"] << '\n';
std::cout<< "Block size: " << to_int(block["size"].as_string()) << " Bytes\n";
std::cout<< "Timestamp: " << to_int(block["timestamp"].as_string()) << '\n';
Expand All @@ -68,14 +70,16 @@ co_main(
auto addr2 = "0x281055afc982d96fab65b3a49cac8b878184cb16";

// Get account balance
auto balance = co_await client[eth_getBalance]({ addr1, block_num });
auto balance = co_await client.async_call(eth_getBalance, { addr1, block_num });

std::cout
<< "Balance: "
<< std::setprecision(18) << dec_float(to_int(balance)) / dec_float("1e18")
<< " ETH\n";

// Estimate gas for a transfer
auto gas_estimate = co_await client[eth_estimateGas](
auto gas_estimate = co_await client.async_call(
eth_estimateGas,
{
json::object
{
Expand All @@ -89,14 +93,15 @@ co_main(
<< to_int(gas_estimate) << '\n';

// Get the current gas price
auto gas_price = co_await client[eth_gasPrice]();
auto gas_price = co_await client.async_call(eth_gasPrice);
std::cout
<< "Gas price: "
<< std::setprecision(3) << dec_float(to_int(gas_price)) / dec_float("1e10")
<< " GWEI\n";

// Get ETH/USD price from a Chainlink oracle
auto eth_usd = co_await client[eth_call](
auto eth_usd = co_await client.async_call(
eth_call,
{
{
{"to", "0x5f4ec3df9cbd43714fe2740f5e3616155c5b8419"}, // ETH/USD price feed
Expand Down Expand Up @@ -148,12 +153,20 @@ main(int, char*[])
{
http_proto::response_parser::config cfg;
cfg.min_buffer = 64 * 1024;
#ifdef BOOST_RTS_HAS_BROTLI
cfg.apply_brotli_decoder = true;
rts::brotli::install_decode_service(rts_ctx);
#endif
#ifdef BOOST_RTS_HAS_ZLIB
cfg.apply_deflate_decoder = true;
cfg.apply_gzip_decoder = true;
rts::zlib::install_inflate_service(rts_ctx);
#endif
http_proto::install_parser_service(rts_ctx, cfg);
}

// Install serializer service with default configuration
http_proto::serializer::config cfg;
http_proto::install_serializer_service(rts_ctx, cfg);
http_proto::install_serializer_service(rts_ctx, {});

// Root certificates used for verification
ssl_ctx.set_default_verify_paths();
Expand Down
Loading
Loading