Skip to content

Commit

Permalink
Merge branch 'VIZ-Blockchain:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
chiliec authored Jan 29, 2024
2 parents 3e64c11 + fde7aea commit 33443d1
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 47 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

We follow [Semantic Versions](https://semver.org/).

## Version 1.0.2

- Add `delegate_vesting_shares` method

## Version 1.0.1

- Support Python 3.12
Expand All @@ -14,4 +18,4 @@ We follow [Semantic Versions](https://semver.org/).

## Version 0.1.0

- Initial release
- Initial release
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python Library for [VIZ](https://github.com/VIZ-Blockchain)

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

**This library is in alpha state, API unstable**
Expand All @@ -27,7 +27,8 @@ Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
brew install openssl
```

and then use the following commands
and then use the following commands:

```sh
export CFLAGS="-I$(brew --prefix openssl)/include"
export LDFLAGS="-L$(brew --prefix openssl)/lib"
Expand All @@ -53,6 +54,7 @@ poetry install
## Usage

Basic read query example:

```python
from viz import Client
from pprint import pprint
Expand All @@ -64,6 +66,7 @@ pprint(viz.info())
```

Direct RPC calls:

```python
viz.rpc.some_rpc_method()
```
88 changes: 54 additions & 34 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "viz-python-lib"
version = "1.0.1"
version = "1.0.2"
description = "Python library for VIZ blockchain"
authors = ["Vladimir Kamarzin <[email protected]>"]
license = "MIT"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def test_negative_withdraw(viz):
viz.withdraw_vesting(-10, account="alice")


def test_negative_delegate_vesting_shares(viz):
with pytest.raises(UnhandledRPCError, match='vesting_shares.amount >= 0: Delegation cannot be negative'):
viz.delegate_vesting_shares(delegator='alice', delegatee='bob', amount=-10)


def test_self_delegate_vesting_shares(viz, default_account):
with pytest.raises(UnhandledRPCError, match='delegator != delegatee: You cannot delegate SHARES to yourself'):
viz.delegate_vesting_shares(delegator=default_account, delegatee=default_account, amount=10)


def test_too_much_beneficiaries(viz, default_account):
beneficiaries = [{"account": default_account, "weight": 50}] * 256

Expand Down
14 changes: 12 additions & 2 deletions tests/test_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def test_proposal_update(viz, default_account):


def test_transfer(viz, default_account):

trx = viz.transfer("null", 1, "VIZ", memo="test_viz", account=default_account)
assert isinstance(trx, dict)
viz.transfer(default_account, 1, "VIZ", memo="#encrypted memo", account=default_account)
Expand All @@ -52,7 +51,14 @@ def test_fixed_award(viz, default_account):
viz.fixed_award(default_account, reward_amount=10, max_energy=50, memo="test_viz", account=default_account)

beneficiaries = [{"account": default_account, "weight": 50}]
viz.fixed_award(default_account, reward_amount=10, max_energy=50, memo="test_viz", beneficiaries=beneficiaries, account=default_account)
viz.fixed_award(
default_account,
reward_amount=10,
max_energy=50,
memo="test_viz",
beneficiaries=beneficiaries,
account=default_account,
)


def test_custom(viz, default_account):
Expand Down Expand Up @@ -132,3 +138,7 @@ def test_create_account(viz):

# referrer
viz.create_account('jimmy8', password='123', creator='alice', referrer='bob')


def test_delegate_vesting_shares(viz):
viz.delegate_vesting_shares(delegator='alice', delegatee='bob', amount=10)
44 changes: 37 additions & 7 deletions viz/viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, DefaultDict, Dict, List, Optional, Union

from graphenecommon.chain import AbstractGrapheneChain
from graphenecommon.exceptions import KeyAlreadyInStoreException
from graphenecommon.exceptions import KeyAlreadyInStoreException, AccountDoesNotExistsException

from vizapi.noderpc import NodeRPC
from vizbase import operations
Expand Down Expand Up @@ -265,7 +265,7 @@ def award(
)

return self.finalizeOp(op, account, "regular")

def fixed_award(
self,
receiver: str,
Expand Down Expand Up @@ -669,13 +669,13 @@ def create_account(
op = operations.Account_create(**op)

return self.finalizeOp(op, creator, "active")

def update_account_profile(
self,
account_name: str,
memo_key: str,
json_meta: Optional[Dict[str, Any]] = None,
) -> dict:
) -> dict:
"""
Update account profile.
Expand All @@ -685,11 +685,11 @@ def update_account_profile(
:param str account_name: (**required**) new account name
:param dict json_meta: Optional meta data for the account
:raises AccountDoesNotExistsException: if the account does not exist
"""

# check if account already exists
# check if the account does not exist
try:
Account(account_name, blockchain_instance=self)
except Exception:
Expand All @@ -705,6 +705,37 @@ def update_account_profile(

return self.finalizeOp(ops=op, account=account_name, permission="active")

def delegate_vesting_shares(self, delegator: str, delegatee: str, amount: float) -> dict:
"""
Delegate vesting SHARES to account.
:param str delegator: account that delegates
:param str delegatee: account to which is delegated
:param float amount: number of SHARES to be delegated
:raises AccountDoesNotExistsException: if the account does not exist
"""

# check if the account does not exist
try:
Account(delegatee, blockchain_instance=self)
except Exception:
raise AccountDoesNotExistsException

op = {
"delegator": delegator,
"delegatee": delegatee,
"vesting_shares": "{:.{prec}f} {asset}".format(
float(amount),
prec=PRECISIONS.get(self.rpc.chain_params["shares_symbol"]),
asset=self.rpc.chain_params["shares_symbol"],
),
}

op = operations.Delegate_vesting_shares(**op)

return self.finalizeOp(op, delegator, "active")

def _store_keys(self, *args):
"""Store private keys to local storage."""
for key in args:
Expand All @@ -714,7 +745,6 @@ def _store_keys(self, *args):
pass

# TODO: Methods to implement:
# - delegate_vesting_shares
# - witness_update
# - chain_properties_update
# - allow / disallow
Expand Down

0 comments on commit 33443d1

Please sign in to comment.