Skip to content

Commit

Permalink
Merge pull request #46 from multiversx/account-01
Browse files Browse the repository at this point in the history
Additional fields on "AccountOnNetwork"
  • Loading branch information
andreibancioiu authored Jun 6, 2024
2 parents a6fb971 + b183fde commit 47ce2b4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
46 changes: 29 additions & 17 deletions multiversx_sdk/network_providers/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,47 @@
class AccountOnNetwork:
def __init__(self):
self.address: IAddress = EmptyAddress()
self.owner_address = EmptyAddress()
self.nonce: int = 0
self.balance: int = 0
self.code: bytes = b''
self.username: str = ''
self.code_hash: str = ''
self.developer_reward = 0
self.code: bytes = b""
self.username: str = ""
self.code_hash: str = ""
self.root_hash: str = ""

@staticmethod
def from_http_response(payload: Dict[str, Any]) -> 'AccountOnNetwork':
def from_http_response(payload: Dict[str, Any]) -> "AccountOnNetwork":
result = AccountOnNetwork()

address = payload.get('address', '')
address = payload.get("address", "")
owner_address = payload.get("ownerAddress", "")

result.address = Address.new_from_bech32(address) if address else EmptyAddress()
result.owner_address = Address.new_from_bech32(owner_address) if owner_address else EmptyAddress()

result.nonce = payload.get("nonce", 0)
result.balance = int(payload.get("balance", 0))
result.developer_reward = int(payload.get("developerReward", 0))

result.nonce = payload.get('nonce', 0)
result.balance = int(payload.get('balance', 0))
result.code = bytes.fromhex(payload.get('code', ''))
result.username = payload.get('username', '')
result.code_hash = payload.get('codeHash', '')
result.code = bytes.fromhex(payload.get("code", ""))
result.username = payload.get("username", "")
result.code_hash = payload.get("codeHash", "")
result.root_hash = payload.get("rootHash", "")

return result

def to_dictionary(self) -> Dict[str, Any]:
return {
"address": self.address.to_bech32(),
"ownerAddress": self.owner_address.to_bech32(),
"nonce": self.nonce,
"balance": self.balance,
"developerReward": self.developer_reward,
"code": self.code.hex(),
"username": self.username,
"codeHash": self.code_hash
"codeHash": self.code_hash,
"rootHash": self.root_hash
}


Expand All @@ -47,10 +59,10 @@ def __init__(self):
self.pending_guardian: Guardian = Guardian()

@staticmethod
def from_http_response(response: Dict[str, Any]) -> 'GuardianData':
def from_http_response(response: Dict[str, Any]) -> "GuardianData":
result = GuardianData()

result.guarded = response.get('guarded', False)
result.guarded = response.get("guarded", False)

if response.get("activeGuardian", None):
result.active_guardian = Guardian.from_http_response(response["activeGuardian"])
Expand All @@ -73,11 +85,11 @@ def __init__(self) -> None:
self.service_uid: str = ""

@staticmethod
def from_http_response(response: Dict[str, Any]) -> 'Guardian':
def from_http_response(response: Dict[str, Any]) -> "Guardian":
result = Guardian()

result.activation_epoch = int(response.get('activationEpoch', 0))
result.address = Address.new_from_bech32(response.get('address', ''))
result.service_uid = response.get('serviceUID', '')
result.activation_epoch = int(response.get("activationEpoch", 0))
result.address = Address.new_from_bech32(response.get("address", ""))
result.service_uid = response.get("serviceUID", "")

return result
14 changes: 13 additions & 1 deletion multiversx_sdk/network_providers/api_network_provider_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import base64

import pytest

from multiversx_sdk.core.address import Address
Expand Down Expand Up @@ -56,8 +58,18 @@ def test_get_account(self):
address = Address.new_from_bech32('erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl')
result = self.api.get_account(address)

assert result.address.to_bech32() == 'erd1487vz5m4zpxjyqw4flwa3xhnkzg4yrr3mkzf5sf0zgt94hjprc8qazcccl'
assert result.address.to_bech32() == address.to_bech32()
assert result.username == ''
assert len(result.code_hash) == 0
assert len(base64.b64decode(result.root_hash)) == 32

address = Address.new_from_bech32('erd1qqqqqqqqqqqqqpgqws44xjx2t056nn79fn29q0rjwfrd3m43396ql35kxy')
result = self.api.get_account(address)

assert result.address.to_bech32() == address.to_bech32()
assert result.username == ''
assert len(base64.b64decode(result.code_hash)) == 32
assert len(base64.b64decode(result.root_hash)) == 32

def test_get_generic_with_bad_address(self):
with pytest.raises(GenericError, match='a bech32 address is expected'):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ allow-direct-references = true

[project]
name = "multiversx-sdk"
version = "0.9.1"
version = "0.9.2"
authors = [
{ name="MultiversX" },
]
Expand Down

0 comments on commit 47ce2b4

Please sign in to comment.