Skip to content
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

Add Python pages #2

Open
wants to merge 1 commit into
base: master
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
3 changes: 3 additions & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions src/integrating-python/building-from-source.md
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions src/integrating-python/index.md
Original file line number Diff line number Diff line change
@@ -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)
72 changes: 72 additions & 0 deletions src/integrating-python/testing.md
Original file line number Diff line number Diff line change
@@ -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)
```