Skip to content

Commit

Permalink
refactor: Remove Operation.type_code(), add Operation.TYPE_CODE (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Oct 22, 2020
1 parent 9c3277d commit c45d087
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 117 deletions.
8 changes: 3 additions & 5 deletions stellar_sdk/operation/account_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class AccountMerge(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.ACCOUNT_MERGE

def __init__(self, destination: str, source: str = None,) -> None:
super().__init__(source)
check_ed25519_public_key(destination)
Expand All @@ -37,16 +39,12 @@ def destination(self, value: str):
self._destination_muxed = None
self._destination = value

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.ACCOUNT_MERGE

def _to_operation_body(self) -> stellar_xdr.OperationBody:
if self._destination_muxed is not None:
destination = self._destination_muxed
else:
destination = Keypair.from_public_key(self._destination).xdr_muxed_account()
body = stellar_xdr.OperationBody(type=self.type_code(), destination=destination)
body = stellar_xdr.OperationBody(type=self.TYPE_CODE, destination=destination)
return body

@classmethod
Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/allow_trust.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class AllowTrust(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.ALLOW_TRUST

def __init__(
self,
trustor: str,
Expand All @@ -68,10 +70,6 @@ def __init__(
else:
self.authorize: TrustLineEntryFlag = authorize

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.ALLOW_TRUST

def _to_operation_body(self) -> stellar_xdr.OperationBody:
Asset.check_if_asset_code_is_valid(self.asset_code)
trustor = Keypair.from_public_key(self.trustor).xdr_account_id()
Expand All @@ -93,7 +91,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody:
)
allow_trust_op = stellar_xdr.AllowTrustOp(trustor, asset, authorize)
body = stellar_xdr.OperationBody(
type=self.type_code(), allow_trust_op=allow_trust_op
type=self.TYPE_CODE, allow_trust_op=allow_trust_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/begin_sponsoring_future_reserves.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,20 @@ class BeginSponsoringFutureReserves(Operation):
:param source: The source account (defaults to transaction source).
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.BEGIN_SPONSORING_FUTURE_RESERVES

def __init__(self, sponsored_id: str, source: str = None) -> None:
super().__init__(source)
check_ed25519_public_key(sponsored_id)
self.sponsored_id: str = sponsored_id

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.BEGIN_SPONSORING_FUTURE_RESERVES

def _to_operation_body(self) -> stellar_xdr.OperationBody:
sponsored_id = Keypair.from_public_key(self.sponsored_id).xdr_account_id()
begin_sponsoring_future_reserves_op = stellar_xdr.BeginSponsoringFutureReservesOp(
sponsored_id=sponsored_id
)
body = stellar_xdr.OperationBody(
type=self.type_code(),
type=self.TYPE_CODE,
begin_sponsoring_future_reserves_op=begin_sponsoring_future_reserves_op,
)
return body
Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/bump_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ class BumpSequence(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.BUMP_SEQUENCE

def __init__(self, bump_to: int, source: str = None) -> None:
super().__init__(source)
self.bump_to: int = bump_to

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.BUMP_SEQUENCE

def _to_operation_body(self) -> stellar_xdr.OperationBody:
sequence = stellar_xdr.SequenceNumber(stellar_xdr.Int64(self.bump_to))
bump_sequence_op = stellar_xdr.BumpSequenceOp(sequence)
body = stellar_xdr.OperationBody(
type=self.type_code(), bump_sequence_op=bump_sequence_op
type=self.TYPE_CODE, bump_sequence_op=bump_sequence_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/change_trust.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ChangeTrust(Operation):

_DEFAULT_LIMIT = "922337203685.4775807"

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CHANGE_TRUST

def __init__(
self, asset: Asset, limit: Union[str, Decimal] = None, source: str = None,
) -> None:
Expand All @@ -39,16 +41,12 @@ def __init__(
check_amount(limit)
self.limit = limit

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.CHANGE_TRUST

def _to_operation_body(self) -> stellar_xdr.OperationBody:
line = self.asset.to_xdr_object()
limit = stellar_xdr.Int64(Operation.to_xdr_amount(self.limit))
change_trust_op = stellar_xdr.ChangeTrustOp(line, limit)
body = stellar_xdr.OperationBody(
type=self.type_code(), change_trust_op=change_trust_op
type=self.TYPE_CODE, change_trust_op=change_trust_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/claim_claimable_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@ class ClaimClaimableBalance(Operation):
:param source: The source account (defaults to transaction source).
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CLAIM_CLAIMABLE_BALANCE

def __init__(self, balance_id: str, source: str = None,) -> None:
super().__init__(source)
self.balance_id: str = balance_id

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.CLAIM_CLAIMABLE_BALANCE

def _to_operation_body(self) -> stellar_xdr.OperationBody:
balance_id_bytes: bytes = binascii.unhexlify(self.balance_id)
balance_id = stellar_xdr.ClaimableBalanceID.from_xdr_bytes(balance_id_bytes)
claim_claimable_balance_op = stellar_xdr.ClaimClaimableBalanceOp(
balance_id=balance_id
)
body = stellar_xdr.OperationBody(
type=self.type_code(), claim_claimable_balance_op=claim_claimable_balance_op
type=self.TYPE_CODE, claim_claimable_balance_op=claim_claimable_balance_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/create_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class CreateAccount(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CREATE_ACCOUNT

def __init__(
self,
destination: str,
Expand All @@ -38,18 +40,14 @@ def __init__(
self.destination: str = destination
self.starting_balance: Union[str, Decimal] = starting_balance

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.CREATE_ACCOUNT

def _to_operation_body(self) -> stellar_xdr.OperationBody:
destination = Keypair.from_public_key(self.destination).xdr_account_id()
starting_balance = stellar_xdr.Int64(
Operation.to_xdr_amount(self.starting_balance)
)
create_account_op = stellar_xdr.CreateAccountOp(destination, starting_balance)
body = stellar_xdr.OperationBody(
type=self.type_code(), create_account_op=create_account_op
type=self.TYPE_CODE, create_account_op=create_account_op
)
return body

Expand Down
7 changes: 2 additions & 5 deletions stellar_sdk/operation/create_claimable_balance.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ class CreateClaimableBalance(Operation):
:param claimants: A list of Claimants.
:param source: The source account (defaults to transaction source).
"""
TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CREATE_CLAIMABLE_BALANCE

def __init__(
self,
Expand All @@ -372,10 +373,6 @@ def __init__(
self.claimants = claimants
self.source = source

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.CREATE_CLAIMABLE_BALANCE

def _to_operation_body(self) -> stellar_xdr.OperationBody:
asset = self.asset.to_xdr_object()
amount = Operation.to_xdr_amount(self.amount)
Expand All @@ -384,7 +381,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody:
asset=asset, amount=stellar_xdr.Int64(amount), claimants=claimants
)
body = stellar_xdr.OperationBody(
type=self.type_code(),
type=self.TYPE_CODE,
create_claimable_balance_op=create_claimable_balance_op,
)
return body
Expand Down
9 changes: 4 additions & 5 deletions stellar_sdk/operation/create_passive_sell_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class CreatePassiveSellOffer(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CREATE_PASSIVE_SELL_OFFER


def __init__(
self,
selling: Asset,
Expand All @@ -56,10 +59,6 @@ def __init__(
self.amount: Union[str, Decimal] = amount
self.price: Union[Price, str, Decimal] = price

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.CREATE_PASSIVE_SELL_OFFER

def _to_operation_body(self) -> stellar_xdr.OperationBody:
selling = self.selling.to_xdr_object()
buying = self.buying.to_xdr_object()
Expand All @@ -75,7 +74,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody:
selling, buying, amount, price
)
body = stellar_xdr.OperationBody(
type=self.type_code(),
type=self.TYPE_CODE,
create_passive_sell_offer_op=create_passive_sell_offer_op,
)
return body
Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/end_sponsoring_future_reserves.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ class EndSponsoringFutureReserves(Operation):
:param source: The source account (defaults to transaction source).
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.END_SPONSORING_FUTURE_RESERVES

def __init__(self, source: str = None) -> None:
super().__init__(source)

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.END_SPONSORING_FUTURE_RESERVES

def _to_operation_body(self) -> stellar_xdr.OperationBody:
body = stellar_xdr.OperationBody(type=self.type_code())
body = stellar_xdr.OperationBody(type=self.TYPE_CODE)
return body

@classmethod
Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/inflation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ class Inflation(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.INFLATION

def __init__(self, source: str = None) -> None:
super().__init__(source)

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.INFLATION

def _to_operation_body(self) -> stellar_xdr.OperationBody:
body = stellar_xdr.OperationBody(type=self.type_code())
body = stellar_xdr.OperationBody(type=self.TYPE_CODE)
return body

@classmethod
Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/manage_buy_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ManageBuyOffer(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.MANAGE_BUY_OFFER

def __init__(
self,
selling: Asset,
Expand All @@ -51,10 +53,6 @@ def __init__(
self.price: Union[Price, str, Decimal] = price
self.offer_id: int = offer_id

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.MANAGE_BUY_OFFER

def _to_operation_body(self) -> stellar_xdr.OperationBody:
selling = self.selling.to_xdr_object()
buying = self.buying.to_xdr_object()
Expand All @@ -71,7 +69,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody:
)

body = stellar_xdr.OperationBody(
type=self.type_code(), manage_buy_offer_op=manage_buy_offer_op
type=self.TYPE_CODE, manage_buy_offer_op=manage_buy_offer_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/manage_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class ManageData(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.MANAGE_DATA

def __init__(
self, data_name: str, data_value: Union[str, bytes, None], source: str = None,
) -> None: # TODO: bytes only?
Expand All @@ -40,10 +42,6 @@ def __init__(
if not valid_data_name_len or not valid_data_val_len:
raise ValueError("Data and value should be <= 64 bytes (ascii encoded).")

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.MANAGE_DATA

def _to_operation_body(self) -> stellar_xdr.OperationBody:
data_name = stellar_xdr.String64(bytes(self.data_name, encoding="utf-8"))
if self.data_value is None:
Expand All @@ -54,7 +52,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody:
manage_data_op = stellar_xdr.ManageDataOp(data_name, data_value)

body = stellar_xdr.OperationBody(
type=self.type_code(), manage_data_op=manage_data_op
type=self.TYPE_CODE, manage_data_op=manage_data_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/manage_sell_offer.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ManageSellOffer(Operation):
"""

TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.MANAGE_SELL_OFFER

def __init__(
self,
selling: Asset,
Expand All @@ -51,10 +53,6 @@ def __init__(
self.price: Union[Price, str, Decimal] = price
self.offer_id: int = offer_id

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
return stellar_xdr.OperationType.MANAGE_SELL_OFFER

def _to_operation_body(self) -> stellar_xdr.OperationBody:
selling = self.selling.to_xdr_object()
buying = self.buying.to_xdr_object()
Expand All @@ -70,7 +68,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody:
selling, buying, amount, price, stellar_xdr.Int64(self.offer_id)
)
body = stellar_xdr.OperationBody(
type=self.type_code(), manage_sell_offer_op=manage_sell_offer_op
type=self.TYPE_CODE, manage_sell_offer_op=manage_sell_offer_op
)
return body

Expand Down
8 changes: 3 additions & 5 deletions stellar_sdk/operation/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Operation(metaclass=ABCMeta):

_ONE = Decimal(10 ** 7)

TYPE_CODE = None

def __init__(self, source: str = None) -> None:
check_source(source)
self._source: Optional[str] = source
Expand All @@ -54,10 +56,6 @@ def source(self, value: str):
self._source_muxed = None
self._source = value

@classmethod
def type_code(cls) -> stellar_xdr.OperationType:
pass # pragma: no cover

@staticmethod
def to_xdr_amount(value: Union[str, Decimal]) -> int:
"""Converts an amount to the appropriate value to send over the network
Expand Down Expand Up @@ -143,7 +141,7 @@ def from_xdr_object(
subclass) instance from.
"""
for sub_cls in cls.__subclasses__():
if sub_cls.type_code() == xdr_object.body.type:
if sub_cls.TYPE_CODE == xdr_object.body.type:
return sub_cls.from_xdr_object(xdr_object)
raise NotImplementedError(
f"Operation of type={xdr_object.body.type} is not implemented."
Expand Down
Loading

0 comments on commit c45d087

Please sign in to comment.