Skip to content

Commit 231395a

Browse files
committed
feat: calculated fee using assign_fee from core
1 parent c87d458 commit 231395a

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed
Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import base64
2+
from typing import cast
23

3-
import algokit_transact
44
import algosdk.transaction
5+
from algokit_transact import (
6+
FeeParams,
7+
PaymentTransactionFields,
8+
Transaction,
9+
TransactionType,
10+
address_from_string,
11+
assign_fee,
12+
encode_transaction_raw,
13+
)
514

615

7-
def build_payment_with_core(
16+
def build_payment_with_core( # noqa: PLR0913
817
sender,
918
sp,
1019
receiver,
@@ -13,35 +22,55 @@ def build_payment_with_core(
1322
note=None,
1423
lease=None,
1524
rekey_to=None,
25+
static_fee=None,
26+
max_fee=None,
27+
extra_fee=None,
1628
) -> algosdk.transaction.PaymentTxn:
17-
txn = algokit_transact.Transaction(
18-
transaction_type=algokit_transact.TransactionType.PAYMENT,
19-
sender=algokit_transact.address_from_string(sender),
20-
# The correct fee will be calculated later based on suggested params and estimated size of the transaction.
21-
fee=sp.fee,
29+
# Determine static fee based on parameters or suggested params
30+
static_fee_value = None
31+
if static_fee is not None:
32+
static_fee_value = static_fee
33+
elif sp.flat_fee:
34+
static_fee_value = sp.fee
35+
36+
txn = Transaction(
37+
transaction_type=TransactionType.PAYMENT,
38+
sender=address_from_string(sender),
39+
fee=static_fee_value,
2240
first_valid=sp.first,
2341
last_valid=sp.last,
2442
genesis_hash=base64.b64decode(sp.gh),
2543
genesis_id=sp.gen,
2644
note=note,
2745
lease=lease,
28-
rekey_to=algokit_transact.address_from_string(rekey_to) if rekey_to else None,
29-
payment=algokit_transact.PaymentTransactionFields(
30-
receiver=algokit_transact.address_from_string(receiver),
46+
rekey_to=address_from_string(rekey_to) if rekey_to else None,
47+
payment=PaymentTransactionFields(
48+
receiver=address_from_string(receiver),
3149
amount=amt,
32-
close_remainder_to=algokit_transact.address_from_string(close_remainder_to) if close_remainder_to else None,
50+
close_remainder_to=address_from_string(close_remainder_to) if close_remainder_to else None,
3351
),
3452
)
3553

36-
size = algokit_transact.estimate_transaction_size(txn)
37-
final_fee: int
38-
if sp.flat_fee:
39-
final_fee = sp.fee
54+
if txn.fee is not None:
55+
# Static fee is already set, encode and return directly
56+
return cast(
57+
algosdk.transaction.PaymentTxn,
58+
algosdk.encoding.msgpack_decode(base64.b64encode(encode_transaction_raw(txn)).decode("utf-8")),
59+
)
4060
else:
61+
# Use assign_fee with fee parameters
4162
min_fee = sp.min_fee or algosdk.constants.MIN_TXN_FEE
42-
final_fee = max(min_fee, sp.flat_fee * size)
43-
txn.fee = final_fee
63+
txn_with_fee = assign_fee(
64+
txn,
65+
FeeParams(
66+
fee_per_byte=sp.fee,
67+
min_fee=min_fee,
68+
max_fee=max_fee,
69+
extra_fee=extra_fee,
70+
),
71+
)
4472

45-
return algosdk.encoding.msgpack_decode(
46-
base64.b64encode(algokit_transact.encode_transaction_raw(txn)).decode("utf-8")
47-
)
73+
return cast(
74+
algosdk.transaction.PaymentTxn,
75+
algosdk.encoding.msgpack_decode(base64.b64encode(encode_transaction_raw(txn_with_fee)).decode("utf-8")),
76+
)

0 commit comments

Comments
 (0)