Skip to content

Commit

Permalink
Merge pull request #36 from nadineloepfe/publish-package
Browse files Browse the repository at this point in the history
Publish package to PyPI
  • Loading branch information
nadineloepfe authored Jan 30, 2025
2 parents 8c61874 + 4c5ad2d commit 92c12c0
Show file tree
Hide file tree
Showing 23 changed files with 836 additions and 199 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Publish to PyPI

on:
push:
tags:
- 'v*.*.*'

jobs:
build-and-publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.9"

- name: Upgrade pip
run: pip install --upgrade pip

- name: Install build & pdm-backend
run: pip install build pdm-backend

- name: Generate Protobuf (if needed)
run: bash ./generate_proto.sh

- name: Build wheel and sdist
run: python -m build

- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ src/hedera_sdk_python/hapi
# Pytest
.pytest_cache

uv.lock
# Lock files
uv.lock
pdm.lock
42 changes: 38 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ This is a Python SDK for interacting with the Hedera Hashgraph platform. It allo
## Table of Contents

- [Installation](#installation)
- [Installing from PyPI](#installing-from-pypi)
- [Installing from Source](#installing-from-source)
- [Local Editable Installation](#local-editable-installation)
- [Environment Setup](#environment-setup)
- [Running Tests](#running-tests)
- [Usage](#usage)
Expand All @@ -31,7 +34,23 @@ This is a Python SDK for interacting with the Hedera Hashgraph platform. It allo

## Installation

0. Install `uv`:
### Installing from PyPI

The latest release of this SDK is published to PyPI. You can install it with:

```
pip install --upgrade pip
pip install hedera-sdk-python
```

This will pull down a stable release along with the required dependencies.


### Installing from Source

You can also clone the repo and install dependencies using uv:

1. Install `uv`:

`uv` is an ultra-fast Python package and project manager. It replaces `pip`, `pip-tools`, `pipx`, `poetry`, `pyenv`,
`virtualenv`, and more.
Expand All @@ -48,27 +67,42 @@ brew install uv

Other installation methods can be found [here](https://docs.astral.sh/uv/getting-started/installation/).

1. Clone this repository:
2. Clone this repository:

```bash
git clone https://github.com/nadineloepfe/hedera_sdk_python.git
cd hedera_sdk_python
```

2. Install dependencies:
3. Install dependencies:

One of the really nice features of `uv` is that it will download and manage the correct version of python and build
with the correct version of python based on the `.python-version` file in the project. This means you don't have to
worry about managing multiple versions of python on your machine!

```bash
uv sync
./generate_proto.sh
./generate_proto.sh # if needed
```

To update to a newer version of the protobuf libraries, edit the `generate_proto.sh` file and change the version number
and then rerun it.


### Local Editable Installation

For active development, you can install the repo in editable mode. That way, changes in your local code are immediately reflected when you import:

```
git clone https://github.com/nadineloepfe/hedera_sdk_python.git
cd hedera_sdk_python
pip install --upgrade pip
pip install -e .
```

Now you can run example scripts like python `examples/account_create.py`, and it will import from your local hedera_sdk_python code.


## Environment Setup

Before using the SDK, you need to configure your environment variables for the operator account and other credentials.
Expand Down
18 changes: 10 additions & 8 deletions examples/account_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
import sys
from dotenv import load_dotenv

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.account.account_create_transaction import AccountCreateTransaction
from hedera_sdk_python.response_code import ResponseCode
from hedera_sdk_python import (
Client,
Network,
AccountId,
PrivateKey,
AccountCreateTransaction,
ResponseCode,
)

load_dotenv()

def create_new_account():
network = Network(network='testnet')
client = Client(network)

operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
client.set_operator(operator_id, operator_key)
Expand All @@ -24,7 +27,7 @@ def create_new_account():
transaction = (
AccountCreateTransaction()
.set_key(new_account_public_key)
.set_initial_balance(100000000) # initial balance of 1 HBAR (in tinybars)
.set_initial_balance(100000000) # 1 HBAR in tinybars
.set_account_memo("My new account")
.freeze_with(client)
)
Expand All @@ -40,7 +43,6 @@ def create_new_account():
raise Exception(f"Transaction failed with status: {status_message}")

new_account_id = receipt.accountId

if new_account_id is not None:
print(f"Account creation successful. New Account ID: {new_account_id}")
print(f"New Account Private Key: {new_account_private_key.to_string()}")
Expand Down
26 changes: 16 additions & 10 deletions examples/query_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import time
from dotenv import load_dotenv

from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_create_transaction import AccountCreateTransaction
from hedera_sdk_python.transaction.transfer_transaction import TransferTransaction
from hedera_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery
from hedera_sdk_python.hbar import Hbar
from hedera_sdk_python.response_code import ResponseCode
from hedera_sdk_python import (
Network,
Client,
AccountId,
PrivateKey,
AccountCreateTransaction,
TransferTransaction,
CryptoGetAccountBalanceQuery,
ResponseCode,
Hbar,
)

load_dotenv()

Expand All @@ -23,21 +25,24 @@ def create_account_and_transfer():
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
client.set_operator(operator_id, operator_key)

# Create new account
new_account_private_key = PrivateKey.generate()
new_account_public_key = new_account_private_key.public_key()
transaction = AccountCreateTransaction(
key=new_account_public_key,
initial_balance=Hbar(10),
initial_balance=Hbar(10), # 10 HBAR
memo="New Account"
).freeze_with(client)
transaction.sign(operator_key)
receipt = transaction.execute(client)
new_account_id = receipt.accountId

# Check balance
balance_query = CryptoGetAccountBalanceQuery().set_account_id(new_account_id)
balance = balance_query.execute(client)
print(f"Initial balance of new account: {balance.hbars} hbars")

# Transfer 5 HBAR from operator to new account
transfer_amount = Hbar(5)
transfer_transaction = (
TransferTransaction()
Expand All @@ -51,6 +56,7 @@ def create_account_and_transfer():

time.sleep(2)

# Check updated balance
updated_balance_query = CryptoGetAccountBalanceQuery().set_account_id(new_account_id)
updated_balance = updated_balance_query.execute(client)
print(f"Updated balance of new account: {updated_balance.hbars} hbars")
Expand Down
34 changes: 16 additions & 18 deletions examples/query_receipt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@
import sys
from dotenv import load_dotenv

from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.client.client import Client
from hedera_sdk_python.transaction.transfer_transaction import TransferTransaction
from hedera_sdk_python.hbar import Hbar
from hedera_sdk_python.query.transaction_get_receipt_query import TransactionGetReceiptQuery
from hedera_sdk_python.response_code import ResponseCode
from hedera_sdk_python import (
Network,
Client,
AccountId,
PrivateKey,
TransferTransaction,
Hbar,
TransactionGetReceiptQuery,
ResponseCode,
)

load_dotenv()

def query_receipt():
network = Network(network='testnet')
client = Client(network)

operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
recipient_id = AccountId.from_string(os.getenv('RECIPIENT_ID'))
Expand All @@ -25,8 +28,8 @@ def query_receipt():

transaction = (
TransferTransaction()
.add_hbar_transfer(operator_id, -Hbar(amount).to_tinybars())
.add_hbar_transfer(recipient_id, Hbar(amount).to_tinybars())
.add_hbar_transfer(operator_id, -Hbar(amount).to_tinybars())
.add_hbar_transfer(recipient_id, Hbar(amount).to_tinybars())
.freeze_with(client)
.sign(operator_key)
)
Expand All @@ -36,14 +39,9 @@ def query_receipt():
print(f"Transaction ID: {transaction_id}")
print(f"Transfer transaction status: {ResponseCode.get_name(receipt.status)}")


receipt_query = (
TransactionGetReceiptQuery()
.set_transaction_id(transaction_id)
)

receipt = receipt_query.execute(client)
print(f"Queried transaction status: {ResponseCode.get_name(receipt.status)}")
receipt_query = TransactionGetReceiptQuery().set_transaction_id(transaction_id)
queried_receipt = receipt_query.execute(client)
print(f"Queried transaction status: {ResponseCode.get_name(queried_receipt.status)}")

if __name__ == "__main__":
query_receipt()
18 changes: 11 additions & 7 deletions examples/query_topic_info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import os
from dotenv import load_dotenv
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.client.client import Client
from hedera_sdk_python.consensus.topic_id import TopicId
from hedera_sdk_python.query.topic_info_query import TopicInfoQuery
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey

from hedera_sdk_python import (
Network,
Client,
AccountId,
PrivateKey,
TopicId,
TopicInfoQuery,
)

load_dotenv()

def query_topic_info():
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
Expand All @@ -17,7 +22,6 @@ def query_topic_info():
client.set_operator(operator_id, operator_key)

query = TopicInfoQuery().set_topic_id(topic_id)

topic_info = query.execute(client)
print("Topic Info:", topic_info)

Expand Down
9 changes: 6 additions & 3 deletions examples/query_topic_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import time
from datetime import datetime
from dotenv import load_dotenv
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.client.client import Client
from hedera_sdk_python.query.topic_message_query import TopicMessageQuery

from hedera_sdk_python import (
Network,
Client,
TopicMessageQuery,
)

load_dotenv()

Expand Down
16 changes: 9 additions & 7 deletions examples/token_associate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import sys
from dotenv import load_dotenv

from hedera_sdk_python.client.client import Client
from hedera_sdk_python.account.account_id import AccountId
from hedera_sdk_python.crypto.private_key import PrivateKey
from hedera_sdk_python.client.network import Network
from hedera_sdk_python.tokens.token_id import TokenId
from hedera_sdk_python.tokens.token_associate_transaction import TokenAssociateTransaction
from hedera_sdk_python import (
Client,
AccountId,
PrivateKey,
Network,
TokenId,
TokenAssociateTransaction,
)

load_dotenv()

Expand All @@ -17,7 +19,7 @@ def associate_token():

recipient_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
recipient_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
token_id = TokenId.from_string('TOKEN_ID')
token_id = TokenId.from_string('TOKEN_ID') # Update as needed

client.set_operator(recipient_id, recipient_key)

Expand Down
Loading

0 comments on commit 92c12c0

Please sign in to comment.