Skip to content
Snippets Groups Projects
Commit c1555c45 authored by Krzysztof Mochocki's avatar Krzysztof Mochocki Committed by Jakub Ziebinski
Browse files

Add transaction factory method

parent e15228d6
No related branches found
No related tags found
1 merge request!230Implementation of the python wax interface
from __future__ import annotations from __future__ import annotations
from datetime import datetime, timezone from datetime import datetime, timedelta, timezone
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from wax._private.core.constants import HIVE_PERCENT_PRECISION_DOT_PLACES, PUBLIC_KEY_ADDRESS_PREFIX from wax._private.core.constants import (
DEFAULT_TRANSACTION_EXPIRATION_TIME,
HIVE_PERCENT_PRECISION_DOT_PLACES,
PUBLIC_KEY_ADDRESS_PREFIX,
)
from wax._private.core.decimal_converter import DecimalConverter from wax._private.core.decimal_converter import DecimalConverter
from wax._private.core.python_price_converter import convert_to_python_price from wax._private.core.python_price_converter import convert_to_python_price
from wax._private.models.asset import ( from wax._private.models.asset import (
...@@ -30,6 +34,7 @@ from wax._private.result_tools import ( ...@@ -30,6 +34,7 @@ from wax._private.result_tools import (
to_python_string, to_python_string,
validate_wax_result, validate_wax_result,
) )
from wax._private.transaction import Transaction
from wax.cpp_python_bridge import ( # type: ignore[attr-defined] from wax.cpp_python_bridge import ( # type: ignore[attr-defined]
calculate_account_hp, calculate_account_hp,
calculate_current_manabar_value, calculate_current_manabar_value,
...@@ -47,12 +52,13 @@ from wax.cpp_python_bridge import ( # type: ignore[attr-defined] ...@@ -47,12 +52,13 @@ from wax.cpp_python_bridge import ( # type: ignore[attr-defined]
suggest_brain_key, suggest_brain_key,
validate_operation, validate_operation,
) )
from wax.interfaces import ChainConfig, IWaxBaseInterface from wax.interfaces import ChainConfig, IWaxBaseInterface, TTimestamp
if TYPE_CHECKING: if TYPE_CHECKING:
from decimal import Decimal from decimal import Decimal
from wax._private.models.basic import AccountName, ChainId, PublicKey, SigDigest, Signature from wax._private.models.basic import AccountName, ChainId, PublicKey, SigDigest, Signature
from wax.interfaces import ITransaction
class WaxBaseApi(IWaxBaseInterface): class WaxBaseApi(IWaxBaseInterface):
...@@ -268,3 +274,10 @@ class WaxBaseApi(IWaxBaseInterface): ...@@ -268,3 +274,10 @@ class WaxBaseApi(IWaxBaseInterface):
return DecimalConverter.convert( return DecimalConverter.convert(
expose_result_as_python_string(result), precision=HIVE_PERCENT_PRECISION_DOT_PLACES expose_result_as_python_string(result), precision=HIVE_PERCENT_PRECISION_DOT_PLACES
) )
def create_transaction_with_tapos(self, tapos_block_id: str, expiration: TTimestamp | None = None) -> ITransaction:
expiration = expiration or DEFAULT_TRANSACTION_EXPIRATION_TIME
if isinstance(expiration, datetime):
expiration = expiration.replace(microsecond=0) - datetime.now(timezone.utc).replace(microsecond=0)
assert isinstance(expiration, timedelta), "Expiration has to be timedelta type"
return Transaction(api=self, tapos_block_id=tapos_block_id, expiration_time=expiration)
...@@ -2,6 +2,7 @@ from __future__ import annotations ...@@ -2,6 +2,7 @@ from __future__ import annotations
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dataclasses import dataclass from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import TYPE_CHECKING, TypeAlias from typing import TYPE_CHECKING, TypeAlias
from typing_extensions import Self from typing_extensions import Self
...@@ -9,7 +10,6 @@ from typing_extensions import Self ...@@ -9,7 +10,6 @@ from typing_extensions import Self
from wax.proto.transaction_pb2 import transaction as proto_transaction from wax.proto.transaction_pb2 import transaction as proto_transaction
if TYPE_CHECKING: if TYPE_CHECKING:
from datetime import datetime
from decimal import Decimal from decimal import Decimal
from beekeepy._interface.abc.asynchronous.wallet import UnlockedWallet as AsyncUnlockedWallet from beekeepy._interface.abc.asynchronous.wallet import UnlockedWallet as AsyncUnlockedWallet
...@@ -31,6 +31,8 @@ if TYPE_CHECKING: ...@@ -31,6 +31,8 @@ if TYPE_CHECKING:
ProtoTransaction: TypeAlias = proto_transaction ProtoTransaction: TypeAlias = proto_transaction
JsonTransaction: TypeAlias = str JsonTransaction: TypeAlias = str
TTimestamp: TypeAlias = datetime | timedelta
ChainConfig: TypeAlias = dict[str, str] ChainConfig: TypeAlias = dict[str, str]
...@@ -574,3 +576,16 @@ class IWaxBaseInterface(ABC): ...@@ -574,3 +576,16 @@ class IWaxBaseInterface(ABC):
Raises: Raises:
WaxValidationFailedError: When passed parameters are wrong. WaxValidationFailedError: When passed parameters are wrong.
""" """
@abstractmethod
def create_transaction_with_tapos(self, tapos_block_id: str, expiration: TTimestamp | None = None) -> ITransaction:
"""
Creates transaction object using basic information from chain.
Args:
tapos_block_id: Block id (mostly head) that transaction should refer to
expiration: time (UTC) till transaction is valid. Default to +1 minute.
Returns:
Transaction object
"""
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment