From 63f8d03a2f8ddb333d998565a92ea1441186f04b Mon Sep 17 00:00:00 2001 From: pratyaksh Date: Tue, 29 Nov 2022 13:01:15 +0530 Subject: [PATCH 1/3] Added onLogs listener --- include/solana.hpp | 7 +++++++ lib/solana.cpp | 25 +++++++++++++++++++++++++ tests/main.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/include/solana.hpp b/include/solana.hpp index 20a4e21..9116680 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -1056,6 +1056,13 @@ class WebSocketSubscriber { /// @brief remove the account change listener for the given id /// @param sub_id the id for which removing subscription is needed void removeAccountChangeListener(RequestIdType sub_id); + + int onLogs(Callback callback, + const Commitment &commitment = Commitment::FINALIZED, + const LogsFilter &logFilter = LogsFilter::ALL, + Callback on_subscibe = nullptr, Callback on_unsubscribe = nullptr); + + void removeOnLogsListener(RequestIdType sub_id); }; } // namespace subscription } // namespace rpc diff --git a/lib/solana.cpp b/lib/solana.cpp index d91e805..eeea584 100644 --- a/lib/solana.cpp +++ b/lib/solana.cpp @@ -1028,6 +1028,31 @@ int WebSocketSubscriber::onAccountChange(const solana::PublicKey &pub_key, void WebSocketSubscriber::removeAccountChangeListener(RequestIdType sub_id) { sess->unsubscribe(sub_id); } + +/*Function to subscribe to logs*/ +int WebSocketSubscriber::onLogs(Callback callback, const Commitment &commitment, + const LogsFilter &logFilter, + Callback on_subscibe, Callback on_unsubscribe) { + // create parameters using the user provided input + json param = {logFilter, {{"commitment", commitment}}}; + + // create a new request content + RequestContent req(curr_id, "logsSubscribe", "logsUnsubscribe", callback, + std::move(param), on_subscibe, on_unsubscribe); + + // subscribe the new request content + sess->subscribe(req); + + // increase the curr_id so that it can be used for the next request content + curr_id += 2; + + return req.id; +} + +void WebSocketSubscriber::removeOnLogsListener(RequestIdType sub_id) { + sess->unsubscribe(sub_id); +} + } // namespace subscription } // namespace rpc } // namespace solana \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index 31637a9..e283fdb 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -983,3 +983,31 @@ TEST_CASE("getBlocks") { CHECK_GE(Blocks[0], startslot); CHECK_LE(Blocks[Blocks.size() - 1], latestslot); } + +TEST_CASE("logs subscribe and unsubscribe") { + const auto connection = solana::rpc::Connection(solana::DEVNET); + solana::rpc::subscription::WebSocketSubscriber sub("api.devnet.solana.com", + "80"); + bool subscribe_called = false; + auto call_on_subscribe = [&subscribe_called](const json& data) { + subscribe_called = true; + }; + + // solana::rpc::subscription::WebSocketSubscriber + // subscribe for account change + int sub_id = sub.onLogs(call_on_subscribe); + // change account data + connection.requestAirdrop(keyPair.publicKey, 50); + // wait for 40 seconds for transaction to process + sleep(40); + // assure that callback has been called + CHECK(subscribe_called); + // stop listening to websocket + sub.removeOnLogsListener(sub_id); + // change account data + connection.requestAirdrop(keyPair.publicKey, 50); + // wait for 40 seconds for transaction to process + sleep(40); + // ensure that callback wasn't called + CHECK(subscribe_called); +} \ No newline at end of file From abad74ce07c5b3eb5ab7f5d5704375522691a6c3 Mon Sep 17 00:00:00 2001 From: pratyaksh Date: Tue, 29 Nov 2022 13:29:14 +0530 Subject: [PATCH 2/3] Added the logsFilter --- include/solana.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/solana.hpp b/include/solana.hpp index 9116680..f1de4f4 100644 --- a/include/solana.hpp +++ b/include/solana.hpp @@ -1021,6 +1021,14 @@ class Connection { /// Websocket requests namespace subscription { +enum class LogsFilter : short { ALL, ALLWITHVOTES }; + +NLOHMANN_JSON_SERIALIZE_ENUM(LogsFilter, + { + {LogsFilter::ALL, "all"}, + {LogsFilter::ALLWITHVOTES, "allWithVotes"}, + }) + /** * Subscribe to an account to receive notifications when the lamports or data * for a given account public key changes From e6bd563a3c563c5de2416681fbcdeab2a09a43cc Mon Sep 17 00:00:00 2001 From: pratyaksh Date: Wed, 30 Nov 2022 19:08:44 +0530 Subject: [PATCH 3/3] Added keypair --- tests/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/main.cpp b/tests/main.cpp index e283fdb..531658d 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -985,6 +985,7 @@ TEST_CASE("getBlocks") { } TEST_CASE("logs subscribe and unsubscribe") { + solana::Keypair keyPair = solana::Keypair::fromFile(KEY_PAIR_FILE); const auto connection = solana::rpc::Connection(solana::DEVNET); solana::rpc::subscription::WebSocketSubscriber sub("api.devnet.solana.com", "80");