Skip to content

Commit

Permalink
Merge pull request #710 from valory-xyz/feat/solana-tx-digest
Browse files Browse the repository at this point in the history
Fix solana send transaction method implementation
  • Loading branch information
angrybayblade committed Feb 5, 2024
2 parents 168bb3c + a0ca3e0 commit cfe4c4a
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions plugins/aea-ledger-solana/aea_ledger_solana/solana.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@


DEFAULT_MAX_SUPPORTED_TRANSACTION_VERSION = 0
DEFAULT_MAX_RETRIES = 3


class SolanaApi(LedgerApi, SolanaHelper):
Expand Down Expand Up @@ -406,9 +407,7 @@ def send_signed_transaction(
:param raise_on_try: whether the method will raise or log on error
:return: tx_digest, if present
"""
tx_digest = self._try_send_signed_transaction(tx_signed, raise_on_try=True)
tx = json.loads(tx_digest)
return tx["result"]
return self._try_send_signed_transaction(tx_signed, raise_on_try=raise_on_try)

@try_decorator("Unable to send transaction: {}", logger_method="warning")
def _try_send_signed_transaction(
Expand All @@ -422,29 +421,9 @@ def _try_send_signed_transaction(
`raise_on_try`: bool flag specifying whether the method will raise or log on error (used by `try_decorator`)
:return: tx_digest, if present
"""
max_supported_transaction_version: Optional[int] = None
if isinstance(tx_signed, dict):
max_supported_transaction_version = tx_signed.pop(
"max_supported_transaction_version", None
)
stxn = self.deserialize_tx(tx=tx_signed)
txn_resp = self._api.send_raw_transaction(bytes(stxn.serialize()))
retries = 15
while True and retries > 0:
try:
tx_digest = str(txn_resp.value)
receipt = self.get_transaction_receipt(
tx_digest,
max_supported_transaction_version=max_supported_transaction_version,
)
if receipt is None:
raise ValueError("Transaction receipt not found.")
break
except ValueError:
time.sleep(1)
retries -= 1

return txn_resp.to_json()
return str(txn_resp.value)

def send_signed_transactions(
self,
Expand All @@ -465,19 +444,22 @@ def get_transaction_receipt(
self,
tx_digest: str,
max_supported_transaction_version: Optional[int] = None,
retries: Optional[int] = None,
raise_on_try: bool = False,
) -> Optional[JSONLike]:
"""
Get the transaction receipt for a transaction digest.
:param tx_digest: the digest associated to the transaction.
:param max_supported_transaction_version: The max transaction version to return in responses.
:param retries: The max amount of retries for fetching the receipt.
:param raise_on_try: whether the method will raise or log on error
:return: the tx receipt, if present
"""
tx_receipt = self._try_get_transaction_receipt(
tx_digest,
max_supported_transaction_version=max_supported_transaction_version,
retries=retries,
raise_on_try=raise_on_try,
)

Expand All @@ -490,28 +472,33 @@ def _try_get_transaction_receipt(
self,
tx_digest: str,
max_supported_transaction_version: Optional[int] = None,
retries: Optional[int] = None,
**_kwargs: Any,
) -> Optional[JSONLike]:
"""
Try get the transaction receipt.
:param tx_digest: the digest associated to the transaction.
:param max_supported_transaction_version: The max transaction version to return in responses.
:param retries: The max amount of retries for fetching the receipt.
:param _kwargs: the keyword arguments. Possible kwargs are:
`raise_on_try`: bool flag specifying whether the method will raise or log on error (used by `try_decorator`)
:return: the tx receipt, if present
"""

tx_receipt = self._api.get_transaction(
Signature.from_string(tx_digest),
max_supported_transaction_version=(
max_supported_transaction_version
or DEFAULT_MAX_SUPPORTED_TRANSACTION_VERSION
),
) # pylint: disable=no-member

tx = json.loads(tx_receipt.to_json())
return tx["result"]
retries = retries or DEFAULT_MAX_RETRIES
while retries > 0:
receipt = self._api.get_transaction(
Signature.from_string(tx_digest),
max_supported_transaction_version=(
max_supported_transaction_version
or DEFAULT_MAX_SUPPORTED_TRANSACTION_VERSION
),
)
if receipt is not None:
return json.loads(receipt.to_json())
retries -= 1
time.sleep(1)
raise ValueError("Transaction receipt not found")

def get_transaction(
self,
Expand Down

0 comments on commit cfe4c4a

Please sign in to comment.