Skip to content

Slot change subscribe and unsubscribe #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions include/solana.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,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 onSlotChange(Callback callback,
const Commitment &commitment = Commitment::FINALIZED,
Callback on_subscibe = nullptr,
Callback on_unsubscribe = nullptr);

void removeSlotChangeListener(RequestIdType sub_id);
};
} // namespace subscription
} // namespace rpc
Expand Down
25 changes: 25 additions & 0 deletions lib/solana.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,31 @@ int WebSocketSubscriber::onAccountChange(const solana::PublicKey &pub_key,
void WebSocketSubscriber::removeAccountChangeListener(RequestIdType sub_id) {
sess->unsubscribe(sub_id);
}

int WebSocketSubscriber::onSlotChange(Callback callback,
const Commitment &commitment,
Callback on_subscibe,
Callback on_unsubscribe) {
// create parameters using the user provided input
json param = json::array();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

param should be an input to this method.


// create a new request content
RequestContent req(curr_id, "slotSubscribe", "slotUnsubscribe", 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::removeSlotChangeListener(RequestIdType sub_id) {
sess->unsubscribe(sub_id);
}

} // namespace subscription
} // namespace rpc
} // namespace solana
19 changes: 19 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,24 @@ TEST_CASE("getBlocks") {
CHECK_LE(Blocks[Blocks.size() - 1], latestslot);
}

TEST_CASE("slot change subscribe and unsubscribe") {
solana::rpc::subscription::WebSocketSubscriber sub("api.devnet.solana.com",
"80");
int num_notif, fin_number;
auto on_callback = [&num_notif](const json&) { ++num_notif; };
auto on_subscribe = [&num_notif](const json&) { num_notif = 0; };
auto on_unsubscribe = [&num_notif, &fin_number](const json&) {
fin_number = num_notif;
};
int sub_id = sub.onSlotChange(on_callback, solana::Commitment::CONFIRMED,
on_subscribe, on_unsubscribe);
sleep(10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid sleep at all costs.

CHECK_GT(num_notif, 0);
sub.removeSlotChangeListener(sub_id);
sleep(10);
CHECK_EQ(num_notif, fin_number);
}

TEST_CASE("getTokenSupply") {
const auto connection = solana::rpc::Connection(solana::MAINNET_BETA);
const auto TokenSupply =
Expand Down Expand Up @@ -1020,3 +1038,4 @@ TEST_CASE("getTokenAccountsByOwner") {
solana::TokenAccountsByOwnerConfig{{}, "jsonParsed"});
CHECK_GT(TokenAccountsByOwner.value.size(), 0);
}