Skip to content
Snippets Groups Projects
Commit 3b44802d authored by Jakub Ziebinski's avatar Jakub Ziebinski Committed by Jakub Ziebinski
Browse files

Update python examples

parent 119a33bd
No related branches found
No related tags found
1 merge request!278Implementation of the IHiveChainInterface
Pipeline #118486 failed
# Python wax library examples
## Visitor
## First you need to install the poetry
```
Recommended installer: https://python-poetry.org/docs/#installing-with-the-official-installer
```
### [visitor_example.py](examples/visitor_example.py)
## Install the dependencies
```bash
poetry install
```
In this file you can see how to create and use your custom visitor for given operation types.
\ No newline at end of file
## Move to the examples directory
```bash
cd examples
```
## Set the environment variables
```markdown
PASSWORD, WALLET_NAME, and HIVED_URL are optional - default values are used if not set
All variables: PASSWORD, WALLET_NAME, ACCOUNT_NAME, TRANSFER_RECEIVER, PRIVATE_KEY, PUBLIC_KEY, HIVED_ADDRESS
```
## Run the examples (create_and_sign_transaction.py in this case)
```bash
python create_and_sign_transaction.py
```
from __future__ import annotations
import os
import asyncio
from beekeepy import AsyncBeekeeper, AsyncSession, AsyncUnlockedWallet, AsyncWallet, Beekeeper
from beekeepy import AsyncUnlockedWallet
from beekeepy.service.asynchronous import create_beekeeper_service
from wax import IOnlineTransaction, WaxChainOptions, create_hive_chain
from wax.cpp_python_bridge import calculate_public_key
from wax.proto.operations import transfer
PASSWORD = "pass"
WALLET_NAME = "alice"
HIVED_ADDRESS = "https://api.hive.blog"
YOUR_ACCOUNT_NAME = ""
TRANSFER_RECEIVER = ""
YOUR_PRIVATE_KEY = ""
YOUR_PUBLIC_KEY = ""
PASSWORD = os.getenv("WALLET_PASSWORD", "password")
WALLET_NAME = os.getenv("WALLET_NAME", "my_wallet")
HIVED_ADDRESS = os.getenv("HIVED_ADDRESS", "https://api.hive.blog")
PRIVATE_KEY = os.getenv("PRIVATE_KEY", "")
PUBLIC_KEY = os.getenv("PUBLIC_KEY", "")
ACCOUNT_NAME = os.getenv("ACCOUNT_NAME", "")
TRANSFER_RECEIVER = os.getenv("TRANSFER_RECEIVER", "")
wax = create_hive_chain(WaxChainOptions(endpoint_url=HIVED_ADDRESS))
async def create_beekeeper_set() -> tuple[AsyncWallet | AsyncUnlockedWallet, AsyncSession, Beekeeper]:
beekeeper = await AsyncBeekeeper.factory()
session = await beekeeper.create_session()
wallet = await session.create_wallet(name=WALLET_NAME, password=PASSWORD) \
if WALLET_NAME not in [w.name for w in await session.wallets_created] \
else await session.open_wallet(name=WALLET_NAME)
return wallet, session, beekeeper
# Create a transaction with automatic acquire the required chain id
async def create_tx() -> IOnlineTransaction:
tx = await wax.create_transaction()
tx.push_operation(
transfer(
from_account=YOUR_ACCOUNT_NAME,
from_account=ACCOUNT_NAME,
to_account=TRANSFER_RECEIVER,
amount=wax.hive.satoshis(1),
memo="hello from wax!")
memo="hello from wax!",
)
)
return tx
# Sign the transaction with the passed wallet
async def sign_tx(unlocked_wallet: AsyncUnlockedWallet, tx: IOnlineTransaction) -> IOnlineTransaction:
if YOUR_PUBLIC_KEY not in await unlocked_wallet.public_keys:
await unlocked_wallet.import_key(private_key=YOUR_PRIVATE_KEY)
if PUBLIC_KEY not in await unlocked_wallet.public_keys:
await unlocked_wallet.import_key(private_key=PRIVATE_KEY)
await tx.sign(wallet=unlocked_wallet, public_key=YOUR_PUBLIC_KEY)
await tx.sign(wallet=unlocked_wallet, public_key=PUBLIC_KEY)
return tx
async def main() -> None:
wallet, session, beekeeper = await create_beekeeper_set()
tx = await create_tx()
unlocked_wallet = wallet if await wallet.is_unlocked() else await wallet.unlock(PASSWORD)
await sign_tx(unlocked_wallet, tx)
await wax.broadcast(tx)
await unlocked_wallet.lock()
await session.close_session()
beekeeper.teardown()
# Create beekeepy set (beekeeper instance, wallet and session) and sign the transaction
async with create_beekeeper_service(wallet_name=WALLET_NAME, password=PASSWORD) as beekeepy:
await sign_tx(beekeepy.wallet, tx)
await wax.broadcast(tx)
asyncio.run(main())
from __future__ import annotations
from google.protobuf.json_format import ParseDict
from wax.proto.operations import comment, limit_order_cancel, recurrent_transfer, vote
......
# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand.
[[package]]
name = "aiohttp"
......@@ -164,12 +164,12 @@ tests-mypy = ["mypy (>=1.11.1)", "pytest-mypy-plugins"]
[[package]]
name = "beekeepy"
version = "0.0.1.dev340+90642d4"
version = "0.0.1.dev340+6ae1979"
description = "All in one package for beekeeper interaction via Python interface."
optional = false
python-versions = ">=3.10,<4.0"
files = [
{file = "beekeepy-0.0.1.dev340+90642d4-py3-none-any.whl", hash = "sha256:686ae2241427915fcda96a6412e1ecc7c498046c797f49ed9773f67ea7822eee"},
{file = "beekeepy-0.0.1.dev340+6ae1979-py3-none-any.whl", hash = "sha256:a3a60269033e1b7bc01fd19c8783a5485bf24e160af057b55faedb7311688900"},
]
[package.dependencies]
......@@ -875,17 +875,17 @@ socks = ["PySocks (>=1.5.6,!=1.5.7,<2.0)"]
[[package]]
name = "wax"
version = "0.3.10.dev579+e53b1fe"
version = "0.3.10.dev581+d566936"
description = ""
optional = false
python-versions = ">=3.10,<4.0"
files = [
{file = "wax-0.3.10.dev579+e53b1fe-cp310-cp310-manylinux_2_35_x86_64.whl", hash = "sha256:521b6cdd0df2b8a5b5b94fde2ea49da39114f129811c30af285ced5318c2b2eb"},
{file = "wax-0.3.10.dev581+d566936-cp310-cp310-manylinux_2_35_x86_64.whl", hash = "sha256:bb28a8bad4507d44925736ec3b868fccad489c6da1e00aca30d7fcd82c6f64a7"},
]
[package.dependencies]
aiohttp = "3.9.1"
beekeepy = "0.0.1.dev340+90642d4"
beekeepy = "0.0.1.dev340+6ae1979"
httpx = {version = "0.23.3", extras = ["http2"]}
loguru = "0.7.2"
protobuf = "4.24.4"
......@@ -1010,4 +1010,4 @@ propcache = ">=0.2.0"
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "edd58440c75d6b3664475dae98e0acaacb38019fbf91cf3200a91fab1dbc12cc"
content-hash = "df5b5919253bc6e252b67711946ce14180a66b1204e71fdd0f96d8897dd7fd32"
......@@ -20,4 +20,4 @@ source = [
[tool.poetry.dependencies]
python = "^3.10"
wax = "0.3.10.dev579+e53b1fe"
wax = "0.3.10.dev581+d566936"
......@@ -3,4 +3,4 @@ set -e
SCRIPTPATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
poetry -C "${SCRIPTPATH}/../../python" run python ${SCRIPTPATH}/visitor_example.py
poetry -C "${SCRIPTPATH}/../../python" run python ${SCRIPTPATH}/examples/visitor_example.py
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment