-
Notifications
You must be signed in to change notification settings - Fork 12
feat: send payment transaction with algokit core #159
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
Changes from 23 commits
32432d9
f15e32e
2700fbc
33a53ee
064c069
13e06d4
5be8767
ae54d01
0362cf9
7dc168a
40c674b
efac078
2ec3993
89b9d89
b7b0f00
9c47442
67940f4
b054765
73b97b0
481f712
24c2a25
cb19b84
eb82ba9
d64633d
1f57a16
add1bde
c87d458
231395a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import base64 | ||
from collections.abc import Iterable | ||
|
||
from algosdk.encoding import msgpack_encode | ||
from algosdk.transaction import GenericSignedTransaction | ||
from algosdk.v2client.algod import AlgodClient | ||
|
||
from algokit_utils import _EXPERIMENTAL_DEPENDENCIES_INSTALLED | ||
|
||
if not _EXPERIMENTAL_DEPENDENCIES_INSTALLED: | ||
raise ImportError( | ||
"Installing experimental dependencies is necessary to use AlgodClientWithCore. " | ||
"Install this package with --group=experimental" | ||
) | ||
|
||
import algokit_algod_api | ||
|
||
|
||
class AlgodClientWithCore: | ||
""" | ||
A decorator for AlgodClient that extends its functionality with algokit_algod_api capabilities. | ||
This class wraps an AlgodClient instance while maintaining the same interface. | ||
""" | ||
|
||
def __init__(self, algod_client: AlgodClient): | ||
""" | ||
Initialize the AlgodClientWithCore with an existing AlgodClient. | ||
|
||
Args: | ||
algod_client: The AlgodClient instance to wrap | ||
""" | ||
self._algod_client = algod_client | ||
|
||
configuration = algokit_algod_api.Configuration( | ||
host=algod_client.algod_address, api_key={"api_key": self._algod_client.algod_token} | ||
) | ||
api_client = algokit_algod_api.ApiClient(configuration) | ||
self._algod_core_client = algokit_algod_api.AlgodApi(api_client=api_client) | ||
|
||
def send_raw_transaction(self, txn): | ||
""" | ||
Override the method to send a raw transaction using algokit_algod_api. | ||
""" | ||
return self._algod_core_client.raw_transaction(base64.b64decode(txn)) | ||
|
||
def send_transactions(self, txns: Iterable[GenericSignedTransaction]): | ||
""" | ||
Override the method to send multiple transactions using algokit_algod_api. | ||
""" | ||
return self.send_raw_transaction( | ||
base64.b64encode(b"".join(base64.b64decode(msgpack_encode(txn)) for txn in txns)) | ||
) | ||
|
||
def __getattr__(self, name): | ||
""" | ||
Delegate all other method calls to the wrapped AlgodClient instance. | ||
""" | ||
return getattr(self._algod_client, name) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import base64 | ||
|
||
import algokit_transact | ||
import algosdk.transaction | ||
|
||
|
||
def build_payment_with_core( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic has been updated in utils-ts to also perform fee calculations via algokit-transact. See algorandfoundation/algokit-utils-ts#407 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated to use |
||
sender, | ||
sp, | ||
receiver, | ||
amt, | ||
close_remainder_to=None, | ||
note=None, | ||
lease=None, | ||
rekey_to=None, | ||
) -> algosdk.transaction.PaymentTxn: | ||
txn = algokit_transact.Transaction( | ||
transaction_type=algokit_transact.TransactionType.PAYMENT, | ||
sender=algokit_transact.address_from_string(sender), | ||
# The correct fee will be calculated later based on suggested params and estimated size of the transaction. | ||
fee=sp.fee, | ||
first_valid=sp.first, | ||
last_valid=sp.last, | ||
genesis_hash=base64.b64decode(sp.gh), | ||
genesis_id=sp.gen, | ||
note=note, | ||
lease=lease, | ||
rekey_to=algokit_transact.address_from_string(rekey_to) if rekey_to else None, | ||
payment=algokit_transact.PaymentTransactionFields( | ||
receiver=algokit_transact.address_from_string(receiver), | ||
amount=amt, | ||
close_remainder_to=algokit_transact.address_from_string(close_remainder_to) if close_remainder_to else None, | ||
), | ||
) | ||
|
||
size = algokit_transact.estimate_transaction_size(txn) | ||
final_fee: int | ||
if sp.flat_fee: | ||
final_fee = sp.fee | ||
else: | ||
min_fee = sp.min_fee or algosdk.constants.MIN_TXN_FEE | ||
final_fee = max(min_fee, sp.flat_fee * size) | ||
txn.fee = final_fee | ||
|
||
return algosdk.encoding.msgpack_decode( | ||
base64.b64encode(algokit_transact.encode_transaction_raw(txn)).decode("utf-8") | ||
) |
Uh oh!
There was an error while loading. Please reload this page.