From 4257da2d9ee024cbbc93e0222e463394cc37cddf Mon Sep 17 00:00:00 2001 From: smartgoo Date: Sat, 26 Oct 2024 09:32:56 -0400 Subject: [PATCH] python section --- src/SUMMARY.md | 3 + .../building-from-source.md | 3 + src/integrating-python/index.md | 12 ++++ src/integrating-python/testing.md | 72 +++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 src/integrating-python/building-from-source.md create mode 100644 src/integrating-python/index.md create mode 100644 src/integrating-python/testing.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 0a7e76c..1afbd45 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -8,6 +8,9 @@ - [Building from Source](./integrating-wasm/building-from-source.md) - [Redistributables](./integrating-wasm/redistributables.md) - [Examples](./integrating-wasm/examples.md) +- [Integrating with Python](./integrating-python/index.md) + - [Building from Source](./integrating-python/building-from-source.md) + - [Testing](./integrating-python/testing.md) - [SDK Examples](./sdk-examples/index.md) - [Explorers](./explorers.md) - [Faucets & Mining](./faucets-mining.md) diff --git a/src/integrating-python/building-from-source.md b/src/integrating-python/building-from-source.md new file mode 100644 index 0000000..fb8afe4 --- /dev/null +++ b/src/integrating-python/building-from-source.md @@ -0,0 +1,3 @@ +# Building from Source + +Instructions for building Python SDK from source can be found in the [Rusty Kaspa respository Python SDK README](https://github.com/aspectron/rusty-kaspa/blob/python/python/README.md) \ No newline at end of file diff --git a/src/integrating-python/index.md b/src/integrating-python/index.md new file mode 100644 index 0000000..d7c0fb3 --- /dev/null +++ b/src/integrating-python/index.md @@ -0,0 +1,12 @@ +# Python SDK + +**Kaspa Python SDK is under active development. Expect issues and breaking changes. Please use accordingly.** + +The Kaspa Python SDK currently provides the following main categories of functionality: +- wRPC Client & Resolver +- Key management +- Transaction generation + +As this module is built from Rusty Kaspa, [PyO3](https://pyo3.rs/latest/) is used to provide bindings to select Rusty Kaspa source. [Maturin](https://www.maturin.rs) is then used to build the Rust source into a native Python extension module. + +This project is a work in progress. As such, it is not currently part of the rusty-kaspa repository. It can currently be found in this Rusty Kaspa fork: [https://github.com/aspectron/rusty-kaspa/tree/python](https://github.com/aspectron/rusty-kaspa/tree/python/python) \ No newline at end of file diff --git a/src/integrating-python/testing.md b/src/integrating-python/testing.md new file mode 100644 index 0000000..0870891 --- /dev/null +++ b/src/integrating-python/testing.md @@ -0,0 +1,72 @@ +# Testing + +Given that Python SDK is new and under active development, it should be used for testing and non-production use cases. + +## Detailed Examples + +Below are a handful of simple examples. More detailed example usage can be found in the Kaspa `python` crate at [https://github.com/aspectron/rusty-kaspa/tree/python/python/examples](https://github.com/aspectron/rusty-kaspa/tree/python/python/examples). + +## Simple Examples + +### RPC Request +```python +import asyncio +from kapsa import Resolver, RpcClient + +async def main(): + # Using Resolver to connect to Kaspa PNN + resolver = Resolver() + client = RpcClient(resolver) + await client.connect() + + print(await client.get_server_info()) + +if __name__ == "__main__": + asyncio.run(main()) +``` + +### RPC Subscription +```python +import asyncio +from kaspa import Resolver, RpcClient + +def handle_block_added_event(event, name, **kwargs): + print(f"{name} | {event}") + +def handle_vcc_event(event, name, **kwargs): + print(f"{name} | {event}") + +async def main(): + # Use Resolver to connect to Kaspa PNN + resolver = Resolver() + client = RpcClient(resolver) + await client.connect() + + # Add event listeners and subscribe + client.add_event_listener("block-added", callback=handle_block_added_event, name="block-handler") + client.add_event_listener("virtual-chain-changed", callback=handle_vcc_event, name="vcc-handler") + await client.subscribe_block_added() + await client.subscribe_virtual_chain_changed(True) + + await asyncio.sleep(5) + + await client.unsubscribe_block_added() + await client.unsubscribe_virtual_chain_changed(True) + + await client.disconnect() + +if __name__ == "__main__": + asyncio.run(main()) +``` + +### Mnemonic generation: +```python +from kaspa import Mnemonic + +if __name__ == "__main__": + # Random mnemonic + mnemonic1 = Mnemonic.random() + + # mnemonic from phrase + mnemonic2 = Mnemonic(phrase=mnemonic1.phrase) +``` \ No newline at end of file