|
| 1 | +import base64 |
| 2 | +from collections.abc import Iterable |
| 3 | + |
| 4 | +import algokit_algod_api |
| 5 | +from algosdk.encoding import msgpack_encode |
| 6 | +from algosdk.transaction import GenericSignedTransaction |
| 7 | +from algosdk.v2client.algod import AlgodClient |
| 8 | + |
| 9 | + |
| 10 | +class AlgodClientWithCore: |
| 11 | + """ |
| 12 | + A decorator for AlgodClient that extends its functionality with algokit_algod_api capabilities. |
| 13 | + This class wraps an AlgodClient instance while maintaining the same interface. |
| 14 | + """ |
| 15 | + |
| 16 | + def __init__(self, algod_client: AlgodClient): |
| 17 | + """ |
| 18 | + Initialize the AlgodClientWithCore with an existing AlgodClient. |
| 19 | +
|
| 20 | + Args: |
| 21 | + algod_client: The AlgodClient instance to wrap |
| 22 | + """ |
| 23 | + self._algod_client = algod_client |
| 24 | + |
| 25 | + configuration = algokit_algod_api.Configuration( |
| 26 | + host=algod_client.algod_address, |
| 27 | + ) |
| 28 | + api_client = algokit_algod_api.ApiClient(configuration) |
| 29 | + api_client.default_headers.update({"X-Algo-API-Token": self._algod_client.algod_token}) |
| 30 | + self._algod_core_client = algokit_algod_api.AlgodApi(api_client=api_client) |
| 31 | + |
| 32 | + def send_raw_transaction(self, txn): |
| 33 | + """ |
| 34 | + Override the method to send a raw transaction using algokit_algod_api. |
| 35 | + """ |
| 36 | + return self._algod_core_client.raw_transaction(base64.b64decode(txn)) |
| 37 | + |
| 38 | + def send_transactions(self, txns: Iterable[GenericSignedTransaction]): |
| 39 | + """ |
| 40 | + Override the method to send multiple transactions using algokit_algod_api. |
| 41 | + """ |
| 42 | + return self.send_raw_transaction( |
| 43 | + base64.b64encode(b"".join(base64.b64decode(msgpack_encode(txn)) for txn in txns)) |
| 44 | + ) |
| 45 | + |
| 46 | + def __getattr__(self, name): |
| 47 | + """ |
| 48 | + Delegate all other method calls to the wrapped AlgodClient instance. |
| 49 | + """ |
| 50 | + return getattr(self._algod_client, name) |
0 commit comments