Skip to content

Commit

Permalink
Merge pull request #2 from MiranDaniel/dev
Browse files Browse the repository at this point in the history
Banano support, unit converstion and more functions
  • Loading branch information
MiranDaniel authored Jun 16, 2021
2 parents ff6f694 + 3bf83ab commit eb67635
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 17 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,6 @@ dmypy.json

# Tests
test.py
testy.py
testy.py
.vscode
.VSCodeCounter
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

***PNRW is a Python Wrapper for the Nano RPC protocol.***

***PNRW supports Banano as well!***

---

![badge](https://img.shields.io/pypi/dm/pnrw?style=for-the-badge)
Expand Down Expand Up @@ -72,6 +74,14 @@ import pnrw
node = pnrw.Node("mynano.ninja/api/node") # Create a new node instance
```

#### Starting code for Banano

```py
import pnrw

node = pnrw.Node("kaliumapi.appditto.com/api", banano=True) # Create a new node instance
```

#### Getting basic node information

```py
Expand All @@ -83,7 +93,7 @@ print(node.block_count()) # Check node block count
print(node.version()["protocol_version"]) # Print protocol version of node
print(node.uptime()) # Print node uptime in seconds
```
<!-- markdownlint-restore -->

#### Showing current account balance

```py
Expand All @@ -97,6 +107,17 @@ myBalance = node.rai_from_raw(balance["balance"]) # Convert from raw to Nano
print(f"I currently have {myBalance} Nano!")
```

#### Convert units

```py
from pnrw import convert

print(convert.convert(123,"nano","knano")) # convert 123 Nano to kNano
print(convert.convert(456,"ban","banoshi")) # convert 456 Banano to banoshi
```

<!-- markdownlint-restore -->

---

## Differences from official commands
Expand Down
5 changes: 3 additions & 2 deletions pnrw/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .pnrwf import Node as N
from .pnrwf import Node

from . import convert
from . import exceptions
Node = N
27 changes: 27 additions & 0 deletions pnrw/convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""
https://nanoo.tools/unit-converter
"""

table = {"raw":1}

def _table(currency="nano"):
if currency.lower() == "nano":
for i, p in enumerate(["G","M","k","","m","u"]):
inRaw = 10 ** (33-(i*3))
table[p+currency] = inRaw
elif currency.lower() == "ban":
for i, p in enumerate(["Gban","Mban","kban","ban","banoshi"]):
if p != "banoshi":
t = 3
else:
t = 11
i = 1
inRaw = 10 ** (33-(i*t))
table[p] = inRaw


def convert(amount, From, To):
return amount*(table[From]/table[To])

_table()
_table("ban")
96 changes: 85 additions & 11 deletions pnrw/pnrwf.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ def _validate_ip(s):


def _validate_address(s):
if s.startswith("nano_") == False and s.startswith("ban_") == False:
if s.startswith("nano_") == False and s.startswith("ban_") == False and s.startswith("xrb_") == False:
raise AddressInvalid(s)
else:
if s.startswith("nano_"):
if len(s) != 65:
raise AddressInvalid(s)
if s.startswith("xrb_"):
if len(s) != 65:
raise AddressInvalid(s)
elif s.startswith("ban_"):
if len(s) != 64:
raise AddressInvalid(s)
Expand Down Expand Up @@ -76,6 +79,9 @@ def __init__(self, ip, port="Default", dontUseHTTPS=False, headers="Default", ba
if headers == "Default":
self.headers = {'Content-type': 'application/json', 'Accept': '*/*',
"Accept-Encoding": "gzip, deflate, br", "Connection": "keep-alive"}
if banano:
self.headers = {
'Content-type': 'application/json', "Connection": "keep-alive"}
else:
self.headers = headers

Expand Down Expand Up @@ -270,8 +276,15 @@ def block_info(self, json_block, Hash):
"subtype": response["subtype"],
}

# blocks
# blocks_info
def blocks_info(self, json_block, hashes):
response = self._request(
{"action": "blocks_info", "json_block": json_block, "hashes": hashes})
return response["blocks"]

def blocks(self, json_block, hashes):
response = self._request(
{"action": "blocks", "json_block": json_block, "hashes": hashes})
return response["blocks"]

def bootstrap(self, address, port):
response = self._request(
Expand Down Expand Up @@ -305,7 +318,7 @@ def bootstrap_status(self):
"attempts": []
}
for i in response["attempts"]:
ret["attempts"].append(i) # WORK ON DATATYPE CONVERSION
ret["attempts"].append(i)

return ret

Expand Down Expand Up @@ -525,15 +538,58 @@ def version(self, account):
"build_info": response["build_info"]
}

# unchecked
def unchecked(self, json_block, count):
response = self._request(
{"action": "unchecked", "json_block": json_block, "count": count})
ret = {}
for i in response["blocks"]:
ret[i] = {
"type": response["blocks"][i]["type"],
"account": response["blocks"][i]["account"],
"previous": response["blocks"][i]["previous"],
"representative": response["blocks"][i]["representative"],
"balance": int(response["blocks"][i]["balance"]),
"link": response["blocks"][i]["link"],
"link_as_account": response["blocks"][i]["link_as_account"],
"signature": response["blocks"][i]["signature"],
"work": response["blocks"][i]["work"]
}
return ret

def unchecked_clear(self):
response = self._request({"action": "unchecked_clear"})
return response["success"]

# unchecked_get
# unchecked_keys
# unopened
def unchecked_get(self, json_block, Hash):
response = self._request(
{"action": "unchecked_get", "json_block": json_block, "hash": Hash})
return {
"modified_timestamp": int(response["modified_timestamp"]),
"contents": {
"type": response["contents"]["type"],
"account": response["contents"]["account"],
"previous": response["contents"]["previous"],
"representative": response["contents"]["representative"],
"balance": int(response["contents"]["balance"]),
"link": response["contents"]["link"],
"link_as_account": response["contents"]["link_as_account"],
"signature": response["contents"]["signature"],
"work": response["contents"]["work"]
}
}

def unchecked_keys(self, json_block, key, count):
response = self._request(
{"action": "unchecked_keys", "json_block": json_block, "key": key, "count": count})
return response["unchecked"]

def unopened(self, account, count):
response = self._request(
{"action": "unopened", "account": account, "count": count})
ret = {}
for i in response["accounts"]:
ret[i] = int(response["accounts"][i])
return ret

def uptime(self):
response = self._request({"action": "uptime"})
Expand All @@ -543,7 +599,14 @@ def work_cancel(self, Hash):
response = self._request({"action": "work_cancel", "hash": Hash})
return response

# work_generate
def work_generate(self, Hash):
response = self._request({"action": "work_generate", "hash": Hash})
return {
"work": response["work"],
"difficulty": response["difficulty"],
"multiplier": float(response["multiplier"]),
"hash": response["hash"]
}

def work_peer_add(self, address, port):
response = self._request(
Expand All @@ -558,7 +621,15 @@ def work_peers_clear(self):
response = self._request({"action": "work_peer_add"})
return response["success"]

# work validate
def work_validate(self, work, Hash):
response = self._request(
{"action": "work_validate", "work": work, "hash": Hash})
return {
"valid_all": int(response["valid_all"]),
"valid_receive": int(response["valid_receive"]),
"difficulty": response["difficulty"],
"multiplier": float(response["multiplier"])
}

def account_create(self, wallet):
response = self._request(
Expand Down Expand Up @@ -681,7 +752,10 @@ def wallet_destroy(self, wallet):
{"action": "wallet_destroy", "wallet": wallet})
return int(response["destroyed"])

# wallet_export
def wallet_export(self, wallet):
response = self._request(
{"action": "wallet_export", "wallet": wallet})
return json.loads(response["json"])

def wallet_frontiers(self, wallet):
response = self._request(
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='pnrw',
version='0.1.2',
version='0.1.4',
description='Python Nanocurrency RPC Wrapper',
long_description=long_description,
long_description_content_type="text/markdown",
Expand All @@ -25,4 +25,4 @@
"PNRW is a Python wrapper for the Nanocurrency RPC protocol"
),
python_requires='>=3.6'
)
)

0 comments on commit eb67635

Please sign in to comment.