Metadata-Version: 2.4
Name: viz-python-lib
Version: 1.4.0
Summary: Python library for VIZ blockchain
License: MIT
License-File: LICENSE.txt
Keywords: viz,python,library,blockchain
Author: Vladimir Kamarzin
Author-email: vvk@vvk.pp.ru
Requires-Python: >=3.10,<4.0
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Dist: aiohttp (>=3.14.5,<4.0.0)
Requires-Dist: docker (>=7.2.0,<8.0.0)
Requires-Dist: funcy (>=2.1,<3.0)
Requires-Dist: graphenelib (>=1.6.1,<2.0.0)
Requires-Dist: toolz (>=1.2.0,<2.0.0)
Project-URL: Repository, https://github.com/VIZ-Blockchain/viz-python-lib
Description-Content-Type: text/markdown

# Python Library for [VIZ](https://github.com/VIZ-Blockchain)

![Tests Status](https://github.com/VIZ-Blockchain/viz-python-lib/actions/workflows/tests.yml/badge.svg)
[![Documentation Status](https://readthedocs.org/projects/viz-python/badge/?version=latest)](https://viz-python.readthedocs.io/en/latest/?badge=latest)

## Usage examples

### Award someone

```python
from viz import Client

node = "wss://node.viz.cx/ws"
viz = Client(node=node, keys=["5...your_private_regular_key..."])

initiator = "your_account"
receiver = "id"
percent = 10.5  # 10.5%
viz.award(receiver, percent, "with love", None, initiator)
```

### Award someone with fixed reward

```python
from viz import Client

node = "wss://node.viz.cx/ws"
viz = Client(node=node, keys=["5...your_private_regular_key..."])

initiator = "your_account"
receiver = "id"
reward_amount = 3.5  # "3.50 VIZ"
max_energy = 30  # 30%
viz.fixed_award(receiver, reward_amount, max_energy, "with fixed reward", None, initiator)
```

### Send a custom operation

```python
from viz import Client

node = "wss://node.viz.cx/ws"
viz = Client(node=node, keys=["5...your_private_regular_key..."])

account = "your_account"
required_regular_auths = [account]
protocol = "color.place"
custom_json = {"x": 35, "y": 70, "color": "#e50000"}
viz.custom(protocol, custom_json, None, required_regular_auths)
```

### Prediction market operations (HF14 / Onix)

All 23 broadcastable `pm_*` operations are available as serializers in
`vizbase.operations`. Build the operation and sign it with `finalizeOp` (the
same low-level path the built-in helpers use), passing the authority the op
requires — `active` for most PM ops, `regular` for `pm_dispute_vote`:

```python
from viz import Client
from vizbase import operations

node = "wss://node.viz.cx/ws"
viz = Client(node=node, keys=["5...your_private_active_key..."])

account = "your_account"

# Place a bet: binary market -> side 0/1, outcome_index -1; multi -> side -1, outcome_index 0..N-1
bet = operations.Pm_place_bet(
    account=account,
    market_id=42,
    side=1,
    outcome_index=-1,
    amount="1.500 VIZ",
    min_tokens=0,  # slippage floor (0 = none)
    mode=0,  # 0 instant, 1 batch
)
viz.finalizeOp(bet, account, "active")
```

Chain governance uses the versioned variant: `versioned_chain_properties_update`
serializes variant 3 (`chain_properties_hf9`), 4 (`chain_properties_hf13`) or 5
(`chain_properties_pm`, the HF14 PM params) automatically from the fields you pass.

### Agent keys (HF15)

An agent is a label plus a public key that may sign an explicit list of a principal's
operations. It never appears in the account's authorities, so check "authority OR agent":

```python
from viz import Client, agent

viz = Client(node="wss://node.viz.cx/ws")

result = agent.check("alice", "VIZ...public_key...", "transfer", blockchain_instance=viz)
# {"allowed": True, "source": "agent", "role": None, "agent": {...row...}, "operation": "transfer"}

# off-chain scope of your own service
row = agent.find("alice", "VIZ...public_key...", viz)
if row and agent.addon_matches(row, "my-service"):
    ...
```

Grant or revoke with `operations.Set_agent_permission` via `finalizeOp(op, account, "active")`.
To broadcast as an agent, sign with the agent key directly: `tx.appendWif(agent_wif)`.

### Get data from custom protocol

```python
from viz import Client
from viz.account import Account
from viz.block import Block
from viz.instance import set_shared_blockchain_instance
import json

viz = Client("wss://node.viz.cx/ws")
set_shared_blockchain_instance(viz)

account_name = "id"
protocol = "V"
account = Account(account_name, protocol=protocol)

counter_inside_protocol = account["custom_sequence"]
last_used_in_block = account["custom_sequence_block_num"]

block = Block(last_used_in_block)

for tx in block["transactions"]:
    for op_type, op_data in tx["operations"]:
        if op_type != "custom":
            continue
        if op_data.get("id") != protocol:
            continue
        if account_name not in op_data.get("required_regular_auths", []):
            continue

        raw_json = op_data.get("json")
        try:
            json_from_protocol = json.loads(raw_json) if raw_json else None
        except json.JSONDecodeError:
            json_from_protocol = None

        print(json_from_protocol)
```

### Any direct RPC call

```python
from viz import Client

viz = Client("wss://node.viz.cx/ws")
viz.rpc.get_dynamic_global_properties()
```

## Installation

Current published version could be installed via

```sh
pip install viz-python-lib
```

Manual installation:

Install [poetry](https://python-poetry.org/docs/)

```sh
cd viz-python-lib/
poetry install
```

## Development

### Dependencies

#### Linux dependencies

```sh
sudo apt-get install libffi-dev libssl-dev python3-dev
```

#### Windows dependencies

Install regular version of [OpenSSL](https://slproweb.com/products/Win32OpenSSL.html) (not Light) suitable for your core processor.

#### MacOS dependencies

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. This means that you will need to install and export some OpenSSL settings yourself, before you can install viz-python-lib:

```sh
brew install openssl
```

and then use the following commands:

```sh
export CFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib"
```

## HF16 compatibility

The block stream reads the current block interval from the node, including the
3-second to 1-second HF16 transition. Block and header API responses retain QC,
timeout and equivocation extensions as JSON; this library does not serialize or
sign block headers. `validator_equivocation` is virtual operation 106.

`committee_worker_create_request` uses the existing transaction wire format.
From HF16 the node limits the maximum requested amount to 1% of `current_supply`
at creation (rounded down to the smallest VIZ unit), rather than a fixed 500,000 VIZ.

