From c45d0874db5feccefe7ba57b7141eb06e064e09b Mon Sep 17 00:00:00 2001 From: Jun Luo <4catcode@gmail.com> Date: Thu, 22 Oct 2020 22:17:10 +0800 Subject: [PATCH] refactor: Remove Operation.type_code(), add Operation.TYPE_CODE (#390) --- stellar_sdk/operation/account_merge.py | 8 ++--- stellar_sdk/operation/allow_trust.py | 8 ++--- .../begin_sponsoring_future_reserves.py | 8 ++--- stellar_sdk/operation/bump_sequence.py | 8 ++--- stellar_sdk/operation/change_trust.py | 8 ++--- .../operation/claim_claimable_balance.py | 8 ++--- stellar_sdk/operation/create_account.py | 8 ++--- .../operation/create_claimable_balance.py | 7 ++-- .../operation/create_passive_sell_offer.py | 9 +++--- .../end_sponsoring_future_reserves.py | 8 ++--- stellar_sdk/operation/inflation.py | 8 ++--- stellar_sdk/operation/manage_buy_offer.py | 8 ++--- stellar_sdk/operation/manage_data.py | 8 ++--- stellar_sdk/operation/manage_sell_offer.py | 8 ++--- stellar_sdk/operation/operation.py | 8 ++--- .../operation/path_payment_strict_receive.py | 7 ++-- .../operation/path_payment_strict_send.py | 7 ++-- stellar_sdk/operation/payment.py | 8 ++--- stellar_sdk/operation/revoke_sponsorship.py | 7 ++-- stellar_sdk/operation/set_options.py | 7 ++-- stellar_sdk/sep/txrep.py | 32 +++++++++---------- stellar_sdk/server.py | 2 +- 22 files changed, 73 insertions(+), 117 deletions(-) diff --git a/stellar_sdk/operation/account_merge.py b/stellar_sdk/operation/account_merge.py index 5b2eae25..c28dfc0b 100644 --- a/stellar_sdk/operation/account_merge.py +++ b/stellar_sdk/operation/account_merge.py @@ -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) @@ -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 diff --git a/stellar_sdk/operation/allow_trust.py b/stellar_sdk/operation/allow_trust.py index d3cda0fd..3e2daad7 100644 --- a/stellar_sdk/operation/allow_trust.py +++ b/stellar_sdk/operation/allow_trust.py @@ -47,6 +47,8 @@ class AllowTrust(Operation): """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.ALLOW_TRUST + def __init__( self, trustor: str, @@ -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() @@ -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 diff --git a/stellar_sdk/operation/begin_sponsoring_future_reserves.py b/stellar_sdk/operation/begin_sponsoring_future_reserves.py index e256535e..842b4ca1 100644 --- a/stellar_sdk/operation/begin_sponsoring_future_reserves.py +++ b/stellar_sdk/operation/begin_sponsoring_future_reserves.py @@ -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 diff --git a/stellar_sdk/operation/bump_sequence.py b/stellar_sdk/operation/bump_sequence.py index 70fa1fb2..22024797 100644 --- a/stellar_sdk/operation/bump_sequence.py +++ b/stellar_sdk/operation/bump_sequence.py @@ -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 diff --git a/stellar_sdk/operation/change_trust.py b/stellar_sdk/operation/change_trust.py index eed3b703..bf7abeaa 100644 --- a/stellar_sdk/operation/change_trust.py +++ b/stellar_sdk/operation/change_trust.py @@ -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: @@ -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 diff --git a/stellar_sdk/operation/claim_claimable_balance.py b/stellar_sdk/operation/claim_claimable_balance.py index e57cda7b..8fe38856 100644 --- a/stellar_sdk/operation/claim_claimable_balance.py +++ b/stellar_sdk/operation/claim_claimable_balance.py @@ -20,14 +20,12 @@ 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) @@ -35,7 +33,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: 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 diff --git a/stellar_sdk/operation/create_account.py b/stellar_sdk/operation/create_account.py index 856ead21..2d9319d5 100644 --- a/stellar_sdk/operation/create_account.py +++ b/stellar_sdk/operation/create_account.py @@ -26,6 +26,8 @@ class CreateAccount(Operation): """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CREATE_ACCOUNT + def __init__( self, destination: str, @@ -38,10 +40,6 @@ 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( @@ -49,7 +47,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: ) 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 diff --git a/stellar_sdk/operation/create_claimable_balance.py b/stellar_sdk/operation/create_claimable_balance.py index d54a2ab7..e18eb5af 100644 --- a/stellar_sdk/operation/create_claimable_balance.py +++ b/stellar_sdk/operation/create_claimable_balance.py @@ -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, @@ -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) @@ -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 diff --git a/stellar_sdk/operation/create_passive_sell_offer.py b/stellar_sdk/operation/create_passive_sell_offer.py index 614313e9..8bd5606a 100644 --- a/stellar_sdk/operation/create_passive_sell_offer.py +++ b/stellar_sdk/operation/create_passive_sell_offer.py @@ -40,6 +40,9 @@ class CreatePassiveSellOffer(Operation): """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.CREATE_PASSIVE_SELL_OFFER + + def __init__( self, selling: Asset, @@ -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() @@ -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 diff --git a/stellar_sdk/operation/end_sponsoring_future_reserves.py b/stellar_sdk/operation/end_sponsoring_future_reserves.py index e71c4f54..93e6bb20 100644 --- a/stellar_sdk/operation/end_sponsoring_future_reserves.py +++ b/stellar_sdk/operation/end_sponsoring_future_reserves.py @@ -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 diff --git a/stellar_sdk/operation/inflation.py b/stellar_sdk/operation/inflation.py index 6e5dabf9..cc08e072 100644 --- a/stellar_sdk/operation/inflation.py +++ b/stellar_sdk/operation/inflation.py @@ -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 diff --git a/stellar_sdk/operation/manage_buy_offer.py b/stellar_sdk/operation/manage_buy_offer.py index e713a554..22f71f9f 100644 --- a/stellar_sdk/operation/manage_buy_offer.py +++ b/stellar_sdk/operation/manage_buy_offer.py @@ -33,6 +33,8 @@ class ManageBuyOffer(Operation): """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.MANAGE_BUY_OFFER + def __init__( self, selling: Asset, @@ -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() @@ -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 diff --git a/stellar_sdk/operation/manage_data.py b/stellar_sdk/operation/manage_data.py index 776423da..cfe87d78 100644 --- a/stellar_sdk/operation/manage_data.py +++ b/stellar_sdk/operation/manage_data.py @@ -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? @@ -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: @@ -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 diff --git a/stellar_sdk/operation/manage_sell_offer.py b/stellar_sdk/operation/manage_sell_offer.py index 6057f0d5..12d8a032 100644 --- a/stellar_sdk/operation/manage_sell_offer.py +++ b/stellar_sdk/operation/manage_sell_offer.py @@ -33,6 +33,8 @@ class ManageSellOffer(Operation): """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.MANAGE_SELL_OFFER + def __init__( self, selling: Asset, @@ -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() @@ -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 diff --git a/stellar_sdk/operation/operation.py b/stellar_sdk/operation/operation.py index 94a6e8f4..9ac6d857 100644 --- a/stellar_sdk/operation/operation.py +++ b/stellar_sdk/operation/operation.py @@ -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 @@ -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 @@ -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." diff --git a/stellar_sdk/operation/path_payment_strict_receive.py b/stellar_sdk/operation/path_payment_strict_receive.py index 4116ec3f..de6978ce 100644 --- a/stellar_sdk/operation/path_payment_strict_receive.py +++ b/stellar_sdk/operation/path_payment_strict_receive.py @@ -28,6 +28,7 @@ class PathPaymentStrictReceive(Operation): :param source: The source account for the payment. Defaults to the transaction's source account. """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.PATH_PAYMENT_STRICT_RECEIVE def __init__( self, @@ -61,10 +62,6 @@ def destination(self, value: str): self._destination_muxed = None self._destination = value - @classmethod - def type_code(cls) -> stellar_xdr.OperationType: - return stellar_xdr.OperationType.PATH_PAYMENT_STRICT_RECEIVE - def _to_operation_body(self) -> stellar_xdr.OperationBody: if self._destination_muxed is not None: destination = self._destination_muxed @@ -83,7 +80,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: path, ) body = stellar_xdr.OperationBody( - type=self.type_code(), + type=self.TYPE_CODE, path_payment_strict_receive_op=path_payment_strict_receive_op, ) return body diff --git a/stellar_sdk/operation/path_payment_strict_send.py b/stellar_sdk/operation/path_payment_strict_send.py index b7b9c666..71246d78 100644 --- a/stellar_sdk/operation/path_payment_strict_send.py +++ b/stellar_sdk/operation/path_payment_strict_send.py @@ -28,6 +28,7 @@ class PathPaymentStrictSend(Operation): :param source: The source account for the payment. Defaults to the transaction's source account. """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.PATH_PAYMENT_STRICT_SEND def __init__( self, @@ -61,10 +62,6 @@ def destination(self, value: str): self._destination_muxed = None self._destination = value - @classmethod - def type_code(cls) -> stellar_xdr.OperationType: - return stellar_xdr.OperationType.PATH_PAYMENT_STRICT_SEND - def _to_operation_body(self) -> stellar_xdr.OperationBody: if self._destination_muxed is not None: destination = self._destination_muxed @@ -83,7 +80,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: path, ) body = stellar_xdr.OperationBody( - type=self.type_code(), + type=self.TYPE_CODE, path_payment_strict_send_op=path_payment_strict_send_op, ) return body diff --git a/stellar_sdk/operation/payment.py b/stellar_sdk/operation/payment.py index b8185db9..f21cf422 100644 --- a/stellar_sdk/operation/payment.py +++ b/stellar_sdk/operation/payment.py @@ -24,6 +24,8 @@ class Payment(Operation): transaction's source account. """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.PAYMENT + def __init__( self, @@ -50,10 +52,6 @@ def destination(self, value: str): self._destination_muxed = None self._destination = value - @classmethod - def type_code(cls) -> stellar_xdr.OperationType: - return stellar_xdr.OperationType.PAYMENT - def _to_operation_body(self) -> stellar_xdr.OperationBody: asset = self.asset.to_xdr_object() if self._destination_muxed is not None: @@ -62,7 +60,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: destination = Keypair.from_public_key(self._destination).xdr_muxed_account() amount = stellar_xdr.Int64(Operation.to_xdr_amount(self.amount)) payment_op = stellar_xdr.PaymentOp(destination, asset, amount) - body = stellar_xdr.OperationBody(type=self.type_code(), payment_op=payment_op) + body = stellar_xdr.OperationBody(type=self.TYPE_CODE, payment_op=payment_op) return body @classmethod diff --git a/stellar_sdk/operation/revoke_sponsorship.py b/stellar_sdk/operation/revoke_sponsorship.py index 2281dd0c..a4f71fa8 100644 --- a/stellar_sdk/operation/revoke_sponsorship.py +++ b/stellar_sdk/operation/revoke_sponsorship.py @@ -113,6 +113,7 @@ class RevokeSponsorship(Operation): :param signer: The sponsored signer. :param source: The source account (defaults to transaction source). """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.REVOKE_SPONSORSHIP def __init__( self, @@ -134,10 +135,6 @@ def __init__( self.claimable_balance_id = claimable_balance_id self.signer = signer - @classmethod - def type_code(cls) -> stellar_xdr.OperationType: - return stellar_xdr.OperationType.REVOKE_SPONSORSHIP - @classmethod def revoke_account_sponsorship(cls, account_id: str, source: str = None): """Create a "revoke sponsorship" operation for an account. @@ -348,7 +345,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: f"{self.revoke_sponsorship_type} is not a valid RevokeSponsorshipType." ) body = stellar_xdr.OperationBody( - type=self.type_code(), revoke_sponsorship_op=revoke_sponsorship_op + type=self.TYPE_CODE, revoke_sponsorship_op=revoke_sponsorship_op ) return body diff --git a/stellar_sdk/operation/set_options.py b/stellar_sdk/operation/set_options.py index 80d01adf..16a2f8f5 100644 --- a/stellar_sdk/operation/set_options.py +++ b/stellar_sdk/operation/set_options.py @@ -67,6 +67,7 @@ class SetOptions(Operation): :param source: The source account (defaults to transaction source). """ + TYPE_CODE: stellar_xdr.OperationType = stellar_xdr.OperationType.SET_OPTIONS def __init__( self, @@ -101,10 +102,6 @@ def __init__( self.home_domain: str = home_domain self.signer: Optional[Signer] = signer - @classmethod - def type_code(cls) -> stellar_xdr.OperationType: - return stellar_xdr.OperationType.SET_OPTIONS - def _to_operation_body(self) -> stellar_xdr.OperationBody: inflation_dest = ( Keypair.from_public_key(self.inflation_dest).xdr_account_id() @@ -156,7 +153,7 @@ def _to_operation_body(self) -> stellar_xdr.OperationBody: signer, ) body = stellar_xdr.OperationBody( - type=self.type_code(), set_options_op=set_options_op + type=self.TYPE_CODE, set_options_op=set_options_op ) return body diff --git a/stellar_sdk/sep/txrep.py b/stellar_sdk/sep/txrep.py index 1f7a2a21..17317ad3 100644 --- a/stellar_sdk/sep/txrep.py +++ b/stellar_sdk/sep/txrep.py @@ -297,54 +297,54 @@ def _get_operation(index, raw_data_map, tx_prefix): raw_data_map, f"{tx_prefix}operations[{index}].sourceAccount" ) operation_type = _get_value(raw_data_map, f"{prefix}type") - if operation_type == AccountMerge.type_code().name: + if operation_type == AccountMerge.TYPE_CODE.name: return _get_account_merge_op(source_account_id, tx_prefix, raw_data_map, index) - elif operation_type == AllowTrust.type_code().name: + elif operation_type == AllowTrust.TYPE_CODE.name: operation_prefix = prefix + "allowTrustOp." return _get_allow_trust_op(source_account_id, operation_prefix, raw_data_map) - elif operation_type == BumpSequence.type_code().name: + elif operation_type == BumpSequence.TYPE_CODE.name: operation_prefix = prefix + "bumpSequenceOp." return _get_bump_sequence_op(source_account_id, operation_prefix, raw_data_map) - elif operation_type == ChangeTrust.type_code().name: + elif operation_type == ChangeTrust.TYPE_CODE.name: operation_prefix = prefix + "changeTrustOp." return _get_change_trust_op(source_account_id, operation_prefix, raw_data_map) - elif operation_type == CreateAccount.type_code().name: + elif operation_type == CreateAccount.TYPE_CODE.name: operation_prefix = prefix + "createAccountOp." return _get_create_account_op(source_account_id, operation_prefix, raw_data_map) - elif operation_type == CreatePassiveSellOffer.type_code().name: + elif operation_type == CreatePassiveSellOffer.TYPE_CODE.name: operation_prefix = prefix + "createPassiveSellOfferOp." return _get_create_passive_sell_offer_op( source_account_id, operation_prefix, raw_data_map ) - elif operation_type == Inflation.type_code().name: + elif operation_type == Inflation.TYPE_CODE.name: return _get_inflation_op(source_account_id) - elif operation_type == ManageBuyOffer.type_code().name: + elif operation_type == ManageBuyOffer.TYPE_CODE.name: operation_prefix = prefix + "manageBuyOfferOp." return _get_manage_buy_offer_op( source_account_id, operation_prefix, raw_data_map ) - elif operation_type == ManageData.type_code().name: + elif operation_type == ManageData.TYPE_CODE.name: operation_prefix = prefix + "manageDataOp." return _get_manage_data_op(source_account_id, operation_prefix, raw_data_map) - elif operation_type == ManageSellOffer.type_code().name: + elif operation_type == ManageSellOffer.TYPE_CODE.name: operation_prefix = prefix + "manageSellOfferOp." return _get_manage_sell_offer_op( source_account_id, operation_prefix, raw_data_map ) - elif operation_type == PathPaymentStrictReceive.type_code().name: + elif operation_type == PathPaymentStrictReceive.TYPE_CODE.name: operation_prefix = prefix + "pathPaymentStrictReceiveOp." return _get_path_payment_strict_receive_op( source_account_id, operation_prefix, raw_data_map ) - elif operation_type == PathPaymentStrictSend.type_code().name: + elif operation_type == PathPaymentStrictSend.TYPE_CODE.name: operation_prefix = prefix + "pathPaymentStrictSendOp." return _get_path_payment_strict_send_op( source_account_id, operation_prefix, raw_data_map ) - elif operation_type == Payment.type_code().name: + elif operation_type == Payment.TYPE_CODE.name: operation_prefix = prefix + "paymentOp." return _get_payment_op(source_account_id, operation_prefix, raw_data_map) - elif operation_type == SetOptions.type_code().name: + elif operation_type == SetOptions.TYPE_CODE.name: operation_prefix = prefix + "setOptionsOp." return _get_set_options_op(source_account_id, operation_prefix, raw_data_map) else: @@ -688,7 +688,7 @@ def _add_operation( index: int, operation: Operation, prefix: str, lines: List[str] ) -> None: prefix = f"{prefix}operations[{index}]." - operation_type = operation.type_code() + operation_type = operation.TYPE_CODE def add_operation_line(key: str, value: Union[str, int]) -> None: _add_line(f"{prefix}{key}", value, lines) @@ -703,7 +703,7 @@ def add_operation_line(key: str, value: Union[str, int]) -> None: def add_body_line( key: str, value: Union[str, int, None], optional: bool = False ) -> None: - operation_type = operation.type_code() + operation_type = operation.TYPE_CODE key = f"body.{_to_camel_case(operation_type.name)}Op.{key}" if optional: present = True if value is not None else False diff --git a/stellar_sdk/server.py b/stellar_sdk/server.py index 813c4cf3..c34cb75b 100644 --- a/stellar_sdk/server.py +++ b/stellar_sdk/server.py @@ -451,7 +451,7 @@ def __get_check_memo_required_destinations( stellar_xdr.OperationType.PATH_PAYMENT_STRICT_SEND, ) for index, operation in enumerate(transaction.operations): - if operation.type_code() in memo_required_operation_code: + if operation.TYPE_CODE in memo_required_operation_code: destination: str = operation.destination else: continue