Skip to content

Commit

Permalink
fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
popenta committed Nov 12, 2024
1 parent 50fd050 commit ae95b63
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
5 changes: 0 additions & 5 deletions multiversx_sdk/core/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@ def is_smart_contract(self) -> bool:
"""Returns whether the address is a smart contract address"""
return self.to_hex().startswith(SC_HEX_PUBKEY_PREFIX)

# this will be removed in v1.0.0; it's here for compatibility reasons with the deprecated transaction builders
# the transaction builders will also be removed in v1.0.0
def serialize(self) -> bytes:
return self.get_public_key()

def __bytes__(self) -> bytes:
return self.get_public_key()

Expand Down
29 changes: 22 additions & 7 deletions multiversx_sdk/core/transaction_on_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,28 @@
from multiversx_sdk.core.transaction_status import TransactionStatus


class EmptyAddress:
class EmptyAddress(Address):
def __init__(self):
...

def to_bech32(self) -> str:
return ""

def to_hex(self) -> str:
return ""

def hex(self) -> str:
return ""

def get_public_key(self) -> bytes:
return b""

def get_hrp(self) -> str:
return ""

def is_smart_contract(self) -> bool:
return False

def __eq__(self, other: object) -> bool:
if not isinstance(other, EmptyAddress):
return False
Expand All @@ -26,8 +41,8 @@ def __init__(self) -> None:
self.round: int = 0
self.epoch: int = 0
self.value: int = 0
self.receiver: Address = EmptyAddress() # type: ignore
self.sender: Address = EmptyAddress() # type: ignore
self.receiver: Address = EmptyAddress()
self.sender: Address = EmptyAddress()
self.gas_limit: int = 0
self.gas_price: int = 0
self.data: str = ""
Expand Down Expand Up @@ -75,7 +90,7 @@ def to_dictionary(self) -> dict[str, Any]:
class TransactionEvent:
def __init__(self,
raw: dict[str, Any] = {},
address: Address = EmptyAddress(), # type:ignore
address: Address = EmptyAddress(),
identifier: str = "",
topics: list[bytes] = [],
data: bytes = b"",
Expand All @@ -90,7 +105,7 @@ def __init__(self,

class TransactionLogs:
def __init__(self,
address: Address = EmptyAddress(), # type:ignore
address: Address = EmptyAddress(),
events: list[TransactionEvent] = []) -> None:
self.address = address
self.events = events
Expand All @@ -99,8 +114,8 @@ def __init__(self,
class SmartContractResult:
def __init__(self,
raw: dict[str, Any] = {},
sender: Address = EmptyAddress(), # type:ignore
receiver: Address = EmptyAddress(), # type:ignore
sender: Address = EmptyAddress(),
receiver: Address = EmptyAddress(),
data: bytes = b"",
logs: TransactionLogs = TransactionLogs()) -> None:
self.raw = raw
Expand Down
49 changes: 23 additions & 26 deletions multiversx_sdk/network_providers/http_resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import base64
from typing import Any, Optional
from typing import Any, Optional, Union

from multiversx_sdk.core.address import Address
from multiversx_sdk.core.code_metadata import CodeMetadata
Expand Down Expand Up @@ -110,23 +110,25 @@ def _transaction_from_network_response(tx_hash: str, response: dict[str, Any]) -


def transaction_logs_from_response(raw_response: dict[str, Any]) -> TransactionLogs:
address = raw_response.get("address", "") or EmptyAddress()
if not isinstance(address, EmptyAddress):
address = Address.new_from_bech32(address)
address = _convert_bech32_to_address(raw_response.get("address", ""))

events = raw_response.get("events", [])
events = [transaction_events_from_response(event) for event in events]

return TransactionLogs(
address=address, # type: ignore
address=address,
events=events
)


def _convert_bech32_to_address(address: str) -> Address:
if address:
return Address.new_from_bech32(address)
return EmptyAddress()


def transaction_events_from_response(raw_response: dict[str, Any]) -> TransactionEvent:
address = raw_response.get("address", "") or EmptyAddress()
if not isinstance(address, EmptyAddress):
address = Address.new_from_bech32(address)
address = _convert_bech32_to_address(raw_response.get("address", ""))

identifier = raw_response.get("identifier", "")
topics = raw_response.get("topics", None)
Expand Down Expand Up @@ -154,7 +156,7 @@ def transaction_events_from_response(raw_response: dict[str, Any]) -> Transactio

return TransactionEvent(
raw=raw_response,
address=address, # type: ignore
address=address,
identifier=identifier,
topics=topics,
data=data,
Expand Down Expand Up @@ -195,20 +197,15 @@ def smart_contract_result_from_proxy_response(raw_response: dict[str, Any]) -> S


def _smart_contract_result_from_response(raw_response: dict[str, Any]) -> SmartContractResult:
sender = raw_response.get("sender", "") or EmptyAddress()
if not isinstance(sender, EmptyAddress):
sender = Address.new_from_bech32(sender)

receiver = raw_response.get("receiver", "") or EmptyAddress()
if not isinstance(receiver, EmptyAddress):
receiver = Address.new_from_bech32(receiver)
sender = _convert_bech32_to_address(raw_response.get("sender", ""))
receiver = _convert_bech32_to_address(raw_response.get("receiver", ""))

logs = transaction_logs_from_response(raw_response.get("logs", {}))

return SmartContractResult(
raw=raw_response,
sender=sender, # type: ignore
receiver=receiver, # type: ignore
sender=sender,
receiver=receiver,
logs=logs
)

Expand Down Expand Up @@ -283,10 +280,7 @@ def account_from_proxy_response(raw_response: dict[str, Any]) -> AccountOnNetwor
block_coordinates = _get_block_coordinates_from_raw_response(raw_response)

address = Address.new_from_bech32(account.get("address", ""))

owner_address = account.get("ownerAddress", "") or None
if owner_address:
owner_address = Address.new_from_bech32(owner_address)
owner_address = _get_address_or_none(account.get("ownerAddress", ""))

nonce = account.get("nonce", 0)
balance = int(account.get("balance", 0))
Expand Down Expand Up @@ -333,12 +327,15 @@ def account_from_proxy_response(raw_response: dict[str, Any]) -> AccountOnNetwor
)


def _get_address_or_none(address: str) -> Union[Address, None]:
if address:
return Address.new_from_bech32(address)
return None


def account_from_api_response(raw_response: dict[str, Any]) -> AccountOnNetwork:
address = Address.new_from_bech32(raw_response.get("address", ""))

owner_address = raw_response.get("ownerAddress", "") or None
if owner_address:
owner_address = Address.new_from_bech32(owner_address)
owner_address = _get_address_or_none(raw_response.get("ownerAddress", ""))

nonce = raw_response.get("nonce", 0)
balance = int(raw_response.get("balance", 0))
Expand Down

0 comments on commit ae95b63

Please sign in to comment.