Skip to content

Commit

Permalink
Merge pull request #139 from kevinheavey/websocket-parsing-fix
Browse files Browse the repository at this point in the history
Fix `parse_websocket_message` when the message is a `jsonParsed` acco…
  • Loading branch information
kevinheavey authored Feb 18, 2025
2 parents 621bed1 + 6989d56 commit 728d94f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

# Unreleased
# [0.26.0] - 2025-02-18

### Fixed

- Fix `parse_websocket_message` when the message is a `jsonParsed` account notification [(#138)](https://github.com/kevinheavey/solders/pull/138)

### Changed

Expand Down
20 changes: 12 additions & 8 deletions crates/rpc-responses/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,13 @@ use solders_rpc_responses_common::{
notification_struct_def, notification_struct_def_contextless, notification_struct_def_no_eq,
notification_struct_def_outer, notification_struct_def_outer_no_eq, AccountMaybeJSON,
AccountNotification, AccountNotificationJsonParsed, AccountNotificationJsonParsedResult,
AccountNotificationResult, BlockStoreError, ProgramNotification, ProgramNotificationJsonParsed,
ProgramNotificationJsonParsedResult, ProgramNotificationResult, ProgramNotificationType,
RootNotification, RpcBlockhash, RpcIdentity, RpcKeyedAccount, RpcKeyedAccountJsonParsed,
RpcKeyedAccountMaybeJSON, RpcLeaderSchedule, RpcResponseContext, RpcSignatureResponse,
RpcTokenAccountBalance, RpcVersionInfo, RpcVoteAccountInfo, RpcVoteAccountStatus,
SignatureNotification, SignatureNotificationResult, SubscriptionResult, UnsubscribeResult,
AccountNotificationResult, AccountNotificationType, BlockStoreError, ProgramNotification,
ProgramNotificationJsonParsed, ProgramNotificationJsonParsedResult, ProgramNotificationResult,
ProgramNotificationType, RootNotification, RpcBlockhash, RpcIdentity, RpcKeyedAccount,
RpcKeyedAccountJsonParsed, RpcKeyedAccountMaybeJSON, RpcLeaderSchedule, RpcResponseContext,
RpcSignatureResponse, RpcTokenAccountBalance, RpcVersionInfo, RpcVoteAccountInfo,
RpcVoteAccountStatus, SignatureNotification, SignatureNotificationResult, SubscriptionResult,
UnsubscribeResult,
};
use solders_rpc_responses_tx_status::RpcConfirmedTransactionStatusWithSignature;
type Slot = u64;
Expand Down Expand Up @@ -611,7 +612,7 @@ pub enum Notification {
AccountNotification {
#[serde(skip_deserializing)]
jsonrpc: solders_rpc_version::V2,
params: AccountNotification,
params: AccountNotificationType,
},
BlockNotification {
#[serde(skip_deserializing)]
Expand Down Expand Up @@ -662,7 +663,10 @@ impl<'py> IntoPyObject<'py> for Notification {

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
Ok((match self {
Self::AccountNotification { params: p, .. } => p.into_bound_py_any(py),
Self::AccountNotification { params: p, .. } => match p {
AccountNotificationType::JsonParsed(x) => x.into_bound_py_any(py),
AccountNotificationType::Binary(x) => x.into_bound_py_any(py),
},
Self::BlockNotification { params: p, .. } => p.into_bound_py_any(py),
Self::LogsNotification { params: p, .. } => p.into_bound_py_any(py),
Self::ProgramNotification { params: p, .. } => p.into_bound_py_any(py),
Expand Down
6 changes: 6 additions & 0 deletions tests/test_rpc_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,12 @@ def test_account_notification_json_parsed() -> None:
assert isinstance(parsed_sub, AccountNotificationJsonParsed)


def test_parse_websocket_message_account_notification_json_parsed() -> None:
raw = '{"jsonrpc":"2.0","method":"accountNotification","params":{"result":{"context":{"slot":317644540},"value":{"lamports":3171111071114,"data":{"program":"spl-token","parsed":{"info":{"isNative":true,"mint":"So11111111111111111111111111111111111111112","owner":"5Q544fKrFoe6tsEbD7S8EmxGTJYAKtTVhAW5Q5pge4j1","rentExemptReserve":{"amount":"2039280","decimals":9,"uiAmount":0.00203928,"uiAmountString":"0.00203928"},"state":"initialized","tokenAmount":{"amount":"3171109031834","decimals":9,"uiAmount":3171.109031834,"uiAmountString":"3171.109031834"}},"type":"account"},"space":165},"owner":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","executable":false,"rentEpoch":18446744073709551615,"space":165}},"subscription":227181}}'
parsed = parse_websocket_message(raw)
assert isinstance(parsed[0], AccountNotificationJsonParsed)


def test_block_notification() -> None:
raw = """{
"jsonrpc": "2.0",
Expand Down

0 comments on commit 728d94f

Please sign in to comment.