Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for CAP-23 #371

Merged
merged 13 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions docs/en/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,25 @@ SetOptions
.. autoclass:: stellar_sdk.operation.set_options.Flag
:members:

CreateClaimableBalance
----------------------
.. autoclass:: stellar_sdk.operation.CreateClaimableBalance
:members: to_xdr_object, from_xdr_object

.. autoclass:: stellar_sdk.operation.Claimant
:members:

.. autoclass:: stellar_sdk.operation.ClaimPredicate
:members:

.. autoclass:: stellar_sdk.operation.create_claimable_balance.ClaimPredicateType
:members:

ClaimClaimableBalance
---------------------
.. autoclass:: stellar_sdk.operation.ClaimClaimableBalance
:members: to_xdr_object, from_xdr_object

Price
^^^^^

Expand Down
19 changes: 19 additions & 0 deletions docs/zh_CN/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,25 @@ SetOptions
.. autoclass:: stellar_sdk.operation.set_options.Flag
:members:

CreateClaimableBalance
----------------------
.. autoclass:: stellar_sdk.operation.CreateClaimableBalance
:members: to_xdr_object, from_xdr_object

.. autoclass:: stellar_sdk.operation.Claimant
:members:

.. autoclass:: stellar_sdk.operation.ClaimPredicate
:members:

.. autoclass:: stellar_sdk.operation.create_claimable_balance.ClaimPredicateType
:members:

ClaimClaimableBalance
---------------------
.. autoclass:: stellar_sdk.operation.ClaimClaimableBalance
:members: to_xdr_object, from_xdr_object

Price
^^^^^

Expand Down
6 changes: 6 additions & 0 deletions stellar_sdk/operation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from .allow_trust import AllowTrust, TrustLineEntryFlag
from .bump_sequence import BumpSequence
from .change_trust import ChangeTrust
from .claim_claimable_balance import ClaimClaimableBalance
from .create_account import CreateAccount
from .create_claimable_balance import CreateClaimableBalance, Claimant, ClaimPredicate
from .create_passive_sell_offer import CreatePassiveSellOffer
from .inflation import Inflation
from .manage_buy_offer import ManageBuyOffer
Expand All @@ -21,7 +23,11 @@
"AllowTrust",
"BumpSequence",
"ChangeTrust",
"ClaimClaimableBalance",
"CreateAccount",
"CreateClaimableBalance",
"Claimant",
"ClaimPredicate",
"CreatePassiveSellOffer",
"Inflation",
"ManageBuyOffer",
Expand Down
59 changes: 59 additions & 0 deletions stellar_sdk/operation/claim_claimable_balance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import base64
import binascii

from .operation import Operation
from ..xdr import Xdr
from ..xdr.StellarXDR_type import ClaimableBalanceID


class ClaimClaimableBalance(Operation):
"""The :class:`ClaimClaimableBalance` object, which represents a ClaimClaimableBalance
operation on Stellar's network.

Claims a ClaimableBalanceEntry and adds the amount of asset on the entry to the source account.

See `Claim Claimable Balance Documentation
<https://developers.stellar.org/docs/start/list-of-operations/#claim-claimable-balance>_`.

Threshold: Low

:param balance_id: The claimable balance id to be claimed.
:param source: The source account (defaults to transaction source).
"""

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

@classmethod
def type_code(cls) -> int:
return Xdr.const.CLAIM_CLAIMABLE_BALANCE

def _to_operation_body(self) -> Xdr.nullclass:
body = Xdr.nullclass()
body.type = Xdr.const.CLAIM_CLAIMABLE_BALANCE

balance_id_bytes: bytes = binascii.unhexlify(self.balance_id)
balance_id = ClaimableBalanceID.from_xdr(base64.b64encode(balance_id_bytes))
claim_claimable_balance_op = Xdr.types.ClaimClaimableBalanceOp(
balanceID=balance_id
)

body.claimClaimableBalanceOp = claim_claimable_balance_op
return body

@classmethod
def from_xdr_object(
cls, operation_xdr_object: Xdr.types.Operation
) -> "ClaimClaimableBalance":
"""Creates a :class:`ClaimClaimableBalance` object from an XDR Operation
object.
"""
source = Operation.get_source_from_xdr_obj(operation_xdr_object)
balance_id = base64.b64decode(
operation_xdr_object.body.claimClaimableBalanceOp.to_xdr()
)
balance_id = binascii.hexlify(balance_id).decode()
op = cls(balance_id=balance_id, source=source)
op._source_muxed = Operation.get_source_muxed_from_xdr_obj(operation_xdr_object)
return op
Loading