From 12739d2f3f37c6dee5f41ea0995082da1f275c36 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Mon, 10 Oct 2022 18:39:41 +0100 Subject: [PATCH 1/5] add missing getters to RpcBlockhash --- src/rpc/responses.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/rpc/responses.rs b/src/rpc/responses.rs index c07a9a2b..fcc43d15 100644 --- a/src/rpc/responses.rs +++ b/src/rpc/responses.rs @@ -1250,7 +1250,9 @@ contextful_resp_eq!(GetLargestAccountsResp, Vec); #[pyclass(module = "solders.rpc.responses", subclass)] pub struct RpcBlockhash { #[serde_as(as = "DisplayFromStr")] + #[pyo3(get)] pub blockhash: SolderHash, + #[pyo3(get)] pub last_valid_block_height: u64, } From 112ab19c626a2176a0c2978353adc8a9171882f5 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Mon, 10 Oct 2022 19:19:31 +0100 Subject: [PATCH 2/5] support arrays in parse_websocket_message --- python/solders/rpc/responses.pyi | 2 +- src/rpc/responses.rs | 31 ++++++++++++++++++++++++++----- tests/test_rpc_responses.py | 18 +++++++++++------- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/python/solders/rpc/responses.pyi b/python/solders/rpc/responses.pyi index 95166c56..f730a0e4 100644 --- a/python/solders/rpc/responses.pyi +++ b/python/solders/rpc/responses.pyi @@ -2244,4 +2244,4 @@ RPCResult = Union[ def batch_to_json(resps: Sequence[RPCResult]) -> str: ... def batch_from_json(raw: str, parsers: Sequence[Any]) -> List[RPCResult]: ... def parse_notification(raw: str) -> Notification: ... -def parse_websocket_message(raw: str) -> WebsocketMessage: ... +def parse_websocket_message(raw: str) -> List[WebsocketMessage]: ... diff --git a/src/rpc/responses.rs b/src/rpc/responses.rs index fcc43d15..e5b115d0 100644 --- a/src/rpc/responses.rs +++ b/src/rpc/responses.rs @@ -11,7 +11,7 @@ use pyo3::{ PyClass, PyTypeInfo, }; use serde::{Deserialize, Serialize}; -use serde_with::{serde_as, DisplayFromStr, FromInto, TryFromInto}; +use serde_with::{serde_as, DisplayFromStr, FromInto, OneOrMany, TryFromInto}; use solana_sdk::{ clock::{Epoch, Slot, UnixTimestamp}, epoch_info::EpochInfo as EpochInfoOriginal, @@ -395,6 +395,16 @@ impl IntoPy for WebsocketMessage { } } +#[serde_as] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +pub struct WebsocketMessages(#[serde_as(deserialize_as = "OneOrMany<_>")] Vec); + +impl IntoPy for WebsocketMessages { + fn into_py(self, py: Python<'_>) -> PyObject { + self.0.into_py(py) + } +} + macro_rules! contextful_struct_def_eq { ($name:ident, $inner:ty) => { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] @@ -2675,7 +2685,7 @@ pub fn parse_notification(msg: &str) -> PyResult { serde_json::from_str(msg).map_err(to_py_err) } -/// Parse a message received by a Solana websocket subscription. +/// Parse a message or array of messages received by a Solana websocket subscription. /// /// Args: /// msg (str): The raw message JSON. @@ -2687,13 +2697,24 @@ pub fn parse_notification(msg: &str) -> PyResult { /// >>> from solders.rpc.responses import parse_websocket_message /// >>> raw = '{ "jsonrpc": "2.0", "method": "rootNotification", "params": { "result": 4, "subscription": 0 } }' /// >>> parse_websocket_message(raw) -/// RootNotification { +/// [RootNotification { /// result: 4, /// subscription: 0, -/// } +/// }] +/// >>> raw_multi = '[{"jsonrpc": "2.0", "result": 0, "id": 1}, {"jsonrpc": "2.0", "result": 1, "id": 2}]' +/// >>> parse_websocket_message(raw_multi) +/// [SubscriptionResult { +/// jsonrpc: TwoPointOh, +/// id: 1, +/// result: 0, +/// }, SubscriptionResult { +/// jsonrpc: TwoPointOh, +/// id: 2, +/// result: 1, +/// }] /// #[pyfunction] -pub fn parse_websocket_message(msg: &str) -> PyResult { +pub fn parse_websocket_message(msg: &str) -> PyResult { serde_json::from_str(msg).map_err(to_py_err) } diff --git a/tests/test_rpc_responses.py b/tests/test_rpc_responses.py index 8724dfdf..3eae1678 100644 --- a/tests/test_rpc_responses.py +++ b/tests/test_rpc_responses.py @@ -2508,14 +2508,18 @@ def test_vote_notification() -> None: def test_parse_ws_message() -> None: raw_err = '{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid param: WrongSize"},"id":1}' parsed_err = parse_websocket_message(raw_err) - assert isinstance(parsed_err, SubscriptionError) - assert isinstance(parsed_err.error, RpcError) + assert isinstance(parsed_err[0], SubscriptionError) + assert isinstance(parsed_err[0].error, RpcError) raw_ok = '{ "jsonrpc": "2.0", "result": 23784, "id": 3 }' parsed_ok = parse_websocket_message(raw_ok) - assert isinstance(parsed_ok, SubscriptionResult) - assert parsed_ok.result == 23784 - assert parsed_ok.id == 3 + assert isinstance(parsed_ok[0], SubscriptionResult) + assert parsed_ok[0].result == 23784 + assert parsed_ok[0].id == 3 raw_notification = '{ "jsonrpc": "2.0", "method": "rootNotification", "params": { "result": 4, "subscription": 0 } }' parsed_notification = parse_websocket_message(raw_notification) - assert isinstance(parsed_notification, RootNotification) - assert parsed_notification.result == 4 + assert isinstance(parsed_notification[0], RootNotification) + assert parsed_notification[0].result == 4 + raw_multi = '[{"jsonrpc": "2.0", "result": 0, "id": 1}, {"jsonrpc": "2.0", "result": 1, "id": 2}]' + parsed_multi = parse_websocket_message(raw_multi) + assert len(parsed_multi) == 2 + assert isinstance(parsed_multi[0], SubscriptionResult) From 0708a08a9cf5eb44623ca24211bf2833f0959f48 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Mon, 10 Oct 2022 19:21:15 +0100 Subject: [PATCH 3/5] update changelog --- CHANGELOG.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bc0706e..bdace857 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,21 @@ # Changelog +## [0.8.0] - 2022-10-10 + +### Changed + +- `parse_websocket_message` now supports parsing an array of messages, and always returns a list [(#15)](https://github.com/kevinheavey/solders/pull/15) + +### Fixed + +- Add missing getters to `RpcBlockhash` [(#15)](https://github.com/kevinheavey/solders/pull/15) + ## [0.7.0] - 2022-10-09 ### Changed -- Replace `parse__maybe_json` funcs with `MaybeJsonParsed` classes. Also fix bugs with parsing mixed responses. -- Make `batch_from_json` pure Rust instead of relying on the Python `from_json` method. +- Replace `parse__maybe_json` funcs with `MaybeJsonParsed` classes. Also fix bugs with parsing mixed responses. [(#14)](https://github.com/kevinheavey/ +- Make `batch_from_json` pure Rust instead of relying on the Python `from_json` method. [(#14)](https://github.com/kevinheavey/solders/pull/14) ## [0.6.0] - 2022-10-05 From 614d3bc6c786138f9aca001ff541aa9ea803c108 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Mon, 10 Oct 2022 19:22:06 +0100 Subject: [PATCH 4/5] =?UTF-8?q?Bump=20version:=200.7.0=20=E2=86=92=200.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .bumpversion.cfg | 2 +- Cargo.toml | 2 +- docs/conf.py | 2 +- pyproject.toml | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 8c188f46..6d550aef 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.7.0 +current_version = 0.8.0 commit = True tag = False diff --git a/Cargo.toml b/Cargo.toml index 47ff40cb..887ae62d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "solders" -version = "0.7.0" +version = "0.8.0" edition = "2021" include = ["/src", "/LICENSE", "/pyproject.toml"] description = "Python binding to the Solana Rust SDK" diff --git a/docs/conf.py b/docs/conf.py index 094f6d3d..70b50c14 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -22,7 +22,7 @@ author = "Kevin Heavey" # The full version, including alpha/beta/rc tags -release = "0.7.0" +release = "0.8.0" # -- General configuration --------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index dc1f7cf1..3f483a15 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "solders" -version = "0.7.0" +version = "0.8.0" description = "Python bindings for Solana Rust tools" authors = ["kevinheavey "] license = "Apache" @@ -29,7 +29,7 @@ build-backend = "maturin" [project] name = "solders" -version = "0.7.0" +version = "0.8.0" description = "Python binding to the Solana Rust SDK" authors = [ {name = "kevinheavey", email = "kevinheavey123@gmail.com"} ] license = {file = "LICENSE"} From 6b316d085e0451da1cf36db18e420f36c105fe84 Mon Sep 17 00:00:00 2001 From: Kevin Heavey Date: Mon, 10 Oct 2022 19:22:14 +0100 Subject: [PATCH 5/5] bump version --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 9f22fd09..a8f38076 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1464,7 +1464,7 @@ dependencies = [ [[package]] name = "solders" -version = "0.7.0" +version = "0.8.0" dependencies = [ "base64 0.13.0", "bincode",