-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathaccount_manager.py
909 lines (775 loc) · 38.8 KB
/
account_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
import os
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
import algosdk
from algosdk import mnemonic
from algosdk.atomic_transaction_composer import TransactionSigner
from algosdk.mnemonic import to_private_key
from algosdk.transaction import LogicSigAccount as AlgosdkLogicSigAccount
from algosdk.transaction import SuggestedParams
from typing_extensions import Self
from algokit_utils.accounts.kmd_account_manager import KmdAccountManager
from algokit_utils.clients.client_manager import ClientManager
from algokit_utils.clients.dispenser_api_client import DispenserAssetName, TestNetDispenserApiClient
from algokit_utils.config import config
from algokit_utils.models.account import (
DISPENSER_ACCOUNT_NAME,
LogicSigAccount,
MultiSigAccount,
MultisigMetadata,
SigningAccount,
TransactionSignerAccount,
)
from algokit_utils.models.amount import AlgoAmount
from algokit_utils.models.transaction import SendParams
from algokit_utils.protocols.account import TransactionSignerAccountProtocol
from algokit_utils.transactions.transaction_composer import (
PaymentParams,
SendAtomicTransactionComposerResults,
TransactionComposer,
)
from algokit_utils.transactions.transaction_sender import SendSingleTransactionResult
logger = config.logger
__all__ = [
"AccountInformation",
"AccountManager",
"EnsureFundedFromTestnetDispenserApiResult",
"EnsureFundedResult",
]
@dataclass(frozen=True, kw_only=True)
class _CommonEnsureFundedParams:
"""
Common parameters for ensure funded responses.
"""
transaction_id: str
amount_funded: AlgoAmount
@dataclass(frozen=True, kw_only=True)
class EnsureFundedResult(SendSingleTransactionResult, _CommonEnsureFundedParams):
"""
Result from performing an ensure funded call.
"""
@dataclass(frozen=True, kw_only=True)
class EnsureFundedFromTestnetDispenserApiResult(_CommonEnsureFundedParams):
"""
Result from performing an ensure funded call using TestNet dispenser API.
"""
@dataclass(frozen=True, kw_only=True)
class AccountInformation:
"""
Information about an Algorand account's current status, balance and other properties.
See `https://developer.algorand.org/docs/rest-apis/algod/#account` for detailed field descriptions.
:ivar str address: The account's address
:ivar AlgoAmount amount: The account's current balance
:ivar AlgoAmount amount_without_pending_rewards: The account's balance without the pending rewards
:ivar AlgoAmount min_balance: The account's minimum required balance
:ivar AlgoAmount pending_rewards: The amount of pending rewards
:ivar AlgoAmount rewards: The amount of rewards earned
:ivar int round: The round for which this information is relevant
:ivar str status: The account's status (e.g., 'Offline', 'Online')
:ivar int|None total_apps_opted_in: Number of applications this account has opted into
:ivar int|None total_assets_opted_in: Number of assets this account has opted into
:ivar int|None total_box_bytes: Total number of box bytes used by this account
:ivar int|None total_boxes: Total number of boxes used by this account
:ivar int|None total_created_apps: Number of applications created by this account
:ivar int|None total_created_assets: Number of assets created by this account
:ivar list[dict]|None apps_local_state: Local state of applications this account has opted into
:ivar int|None apps_total_extra_pages: Number of extra pages allocated to applications
:ivar dict|None apps_total_schema: Total schema for all applications
:ivar list[dict]|None assets: Assets held by this account
:ivar str|None auth_addr: If rekeyed, the authorized address
:ivar int|None closed_at_round: Round when this account was closed
:ivar list[dict]|None created_apps: Applications created by this account
:ivar list[dict]|None created_assets: Assets created by this account
:ivar int|None created_at_round: Round when this account was created
:ivar bool|None deleted: Whether this account is deleted
:ivar bool|None incentive_eligible: Whether this account is eligible for incentives
:ivar int|None last_heartbeat: Last heartbeat round for this account
:ivar int|None last_proposed: Last round this account proposed a block
:ivar dict|None participation: Participation information for this account
:ivar int|None reward_base: Base reward for this account
:ivar str|None sig_type: Signature type for this account
"""
address: str
amount: AlgoAmount
amount_without_pending_rewards: AlgoAmount
min_balance: AlgoAmount
pending_rewards: AlgoAmount
rewards: AlgoAmount
round: int
status: str
total_apps_opted_in: int | None = None
total_assets_opted_in: int | None = None
total_box_bytes: int | None = None
total_boxes: int | None = None
total_created_apps: int | None = None
total_created_assets: int | None = None
apps_local_state: list[dict] | None = None
apps_total_extra_pages: int | None = None
apps_total_schema: dict | None = None
assets: list[dict] | None = None
auth_addr: str | None = None
closed_at_round: int | None = None
created_apps: list[dict] | None = None
created_assets: list[dict] | None = None
created_at_round: int | None = None
deleted: bool | None = None
incentive_eligible: bool | None = None
last_heartbeat: int | None = None
last_proposed: int | None = None
participation: dict | None = None
reward_base: int | None = None
sig_type: str | None = None
class AccountManager:
"""
Creates and keeps track of signing accounts that can sign transactions for a sending address.
This class provides functionality to create, track, and manage various types of accounts including
mnemonic-based, rekeyed, multisig, and logic signature accounts.
:param client_manager: The ClientManager client to use for algod and kmd clients
:example:
>>> account_manager = AccountManager(client_manager)
"""
def __init__(self, client_manager: ClientManager):
self._client_manager = client_manager
self._kmd_account_manager = KmdAccountManager(client_manager)
self._accounts = dict[str, TransactionSignerAccountProtocol]()
self._default_signer: TransactionSigner | None = None
@property
def kmd(self) -> KmdAccountManager:
return self._kmd_account_manager
def set_default_signer(self, signer: TransactionSigner | TransactionSignerAccountProtocol) -> Self:
"""
Sets the default signer to use if no other signer is specified.
If this isn't set and a transaction needs signing for a given sender
then an error will be thrown from `get_signer` / `get_account`.
:param signer: A `TransactionSigner` signer to use.
:returns: The `AccountManager` so method calls can be chained
:example:
>>> signer_account = account_manager.random()
>>> account_manager.set_default_signer(signer_account.signer)
>>> # When signing a transaction, if there is no signer registered for the sender
>>> # then the default signer will be used
>>> signer = account_manager.get_signer("{SENDERADDRESS}")
"""
self._default_signer = signer if isinstance(signer, TransactionSigner) else signer.signer
return self
def set_signer(self, sender: str, signer: TransactionSigner) -> Self:
"""
Tracks the given `TransactionSigner` against the given sender address for later signing.
:param sender: The sender address to use this signer for
:param signer: The `TransactionSigner` to sign transactions with for the given sender
:returns: The `AccountManager` instance for method chaining
:example:
>>> account_manager.set_signer("SENDERADDRESS", transaction_signer)
"""
self._accounts[sender] = TransactionSignerAccount(address=sender, signer=signer)
return self
def set_signers(self, *, another_account_manager: "AccountManager", overwrite_existing: bool = True) -> Self:
"""
Merges the given `AccountManager` into this one.
:param another_account_manager: The `AccountManager` to merge into this one
:param overwrite_existing: Whether to overwrite existing signers in this manager
:returns: The `AccountManager` instance for method chaining
"""
self._accounts = (
{**self._accounts, **another_account_manager._accounts} # noqa: SLF001
if overwrite_existing
else {**another_account_manager._accounts, **self._accounts} # noqa: SLF001
)
return self
def set_signer_from_account(self, account: TransactionSignerAccountProtocol) -> Self:
"""
Tracks the given account for later signing.
Note: If you are generating accounts via the various methods on `AccountManager`
(like `random`, `from_mnemonic`, `logic_sig`, etc.) then they automatically get tracked.
:param account: The account to register
:returns: The `AccountManager` instance for method chaining
:example:
>>> account_manager = AccountManager(client_manager)
>>> account_manager.set_signer_from_account(SigningAccount(private_key=algosdk.account.generate_account()[0]))
>>> account_manager.set_signer_from_account(LogicSigAccount(AlgosdkLogicSigAccount(program, args)))
>>> account_manager.set_signer_from_account(MultiSigAccount(multisig_params, [account1, account2]))
""" # noqa: E501
self._accounts[account.address] = account
return self
def get_signer(self, sender: str | TransactionSignerAccountProtocol) -> TransactionSigner:
"""
Returns the `TransactionSigner` for the given sender address.
If no signer has been registered for that address then the default signer is used if registered.
:param sender: The sender address or account
:returns: The `TransactionSigner`
:raises ValueError: If no signer is found and no default signer is set
:example:
>>> signer = account_manager.get_signer("SENDERADDRESS")
"""
signer = self._accounts.get(self._get_address(sender)) or self._default_signer
if not signer:
raise ValueError(f"No signer found for address {sender}")
return signer if isinstance(signer, TransactionSigner) else signer.signer
def get_account(self, sender: str) -> TransactionSignerAccountProtocol:
"""
Returns the `TransactionSignerAccountProtocol` for the given sender address.
:param sender: The sender address
:returns: The `TransactionSignerAccountProtocol`
:raises ValueError: If no account is found or if the account is not a regular account
:example:
>>> sender = account_manager.random().address
>>> # ...
>>> # Returns the `TransactionSignerAccountProtocol` for `sender` that has previously been registered
>>> account = account_manager.get_account(sender)
"""
account = self._accounts.get(sender)
if not account:
raise ValueError(f"No account found for address {sender}")
if not isinstance(account, SigningAccount):
raise ValueError(f"Account {sender} is not a regular account")
return account
def get_information(self, sender: str | TransactionSignerAccountProtocol) -> AccountInformation:
"""
Returns the given sender account's current status, balance and spendable amounts.
See `<https://developer.algorand.org/docs/rest-apis/algod/#get-v2accountsaddress>`_
for response data schema details.
:param sender: The address or account compliant with `TransactionSignerAccountProtocol` protocol to look up
:returns: The account information
:example:
>>> address = "XBYLS2E6YI6XXL5BWCAMOA4GTWHXWENZMX5UHXMRNWWUQ7BXCY5WC5TEPA"
>>> account_info = account_manager.get_information(address)
"""
info = self._client_manager.algod.account_info(self._get_address(sender))
assert isinstance(info, dict)
info = {k.replace("-", "_"): v for k, v in info.items()}
for key, value in info.items():
if key in ("amount", "amount_without_pending_rewards", "min_balance", "pending_rewards", "rewards"):
info[key] = AlgoAmount.from_micro_algo(value)
return AccountInformation(**info)
def _register_account(self, private_key: str, address: str | None = None) -> SigningAccount:
"""
Helper method to create and register an account with its signer.
:param private_key: The private key for the account
:param address: The address for the account
:returns: The registered Account instance
"""
address = address or str(algosdk.account.address_from_private_key(private_key))
account = SigningAccount(private_key=private_key, address=address)
self._accounts[address or account.address] = TransactionSignerAccount(
address=account.address, signer=account.signer
)
return account
def _register_logicsig(self, program: bytes, args: list[bytes] | None = None) -> LogicSigAccount:
"""
Helper method to create and register a logic signature account.
:param program: The bytes that make up the compiled logic signature
:param args: The (binary) arguments to pass into the logic signature
:returns: The registered AlgosdkLogicSigAccount instance
"""
logic_sig = LogicSigAccount(AlgosdkLogicSigAccount(program, args))
self._accounts[logic_sig.address] = logic_sig
return logic_sig
def _register_multisig(self, metadata: MultisigMetadata, signing_accounts: list[SigningAccount]) -> MultiSigAccount:
"""
Helper method to create and register a multisig account.
:param metadata: The metadata for the multisig account
:param signing_accounts: The list of accounts that are present to sign
:returns: The registered MultisigAccount instance
"""
msig_account = MultiSigAccount(metadata, signing_accounts)
self._accounts[str(msig_account.address)] = MultiSigAccount(metadata, signing_accounts)
return msig_account
def from_mnemonic(self, *, mnemonic: str, sender: str | None = None) -> SigningAccount:
"""
Tracks and returns an Algorand account with secret key loaded by taking the mnemonic secret.
:param mnemonic: The mnemonic secret representing the private key of an account
:param sender: Optional address to use as the sender
:returns: The account
.. warning::
Be careful how the mnemonic is handled. Never commit it into source control and ideally load it
from the environment (ideally via a secret storage service) rather than the file system.
:example:
>>> account = account_manager.from_mnemonic("mnemonic secret ...")
"""
return self._register_account(to_private_key(mnemonic), sender)
def from_environment(self, name: str, fund_with: AlgoAmount | None = None) -> SigningAccount:
"""
Tracks and returns an Algorand account with private key loaded by convention from environment variables.
This allows you to write code that will work seamlessly in production and local development (LocalNet)
without manual config locally (including when you reset the LocalNet).
:param name: The name identifier of the account
:param fund_with: Optional amount to fund the account with when it gets created
(when targeting LocalNet)
:returns: The account
:raises ValueError: If environment variable {NAME}_MNEMONIC is missing when looking for account {NAME}
.. note::
Convention:
* **Non-LocalNet:** will load `{NAME}_MNEMONIC` as a mnemonic secret.
If `{NAME}_SENDER` is defined then it will use that for the sender address
(i.e. to support rekeyed accounts)
* **LocalNet:** will load the account from a KMD wallet called {NAME} and if that wallet doesn't exist
it will create it and fund the account for you
:example:
>>> # If you have a mnemonic secret loaded into `MY_ACCOUNT_MNEMONIC` then you can call:
>>> account = account_manager.from_environment('MY_ACCOUNT')
>>> # If that code runs against LocalNet then a wallet called `MY_ACCOUNT` will automatically be created
>>> # with an account that is automatically funded with the specified amount from the LocalNet dispenser
"""
account_mnemonic = os.getenv(f"{name.upper()}_MNEMONIC")
if account_mnemonic:
private_key = mnemonic.to_private_key(account_mnemonic)
return self._register_account(private_key)
if self._client_manager.is_localnet():
kmd_account = self._kmd_account_manager.get_or_create_wallet_account(name, fund_with)
return self._register_account(kmd_account.private_key)
raise ValueError(f"Missing environment variable {name.upper()}_MNEMONIC when looking for account {name}")
def from_kmd(
self, name: str, predicate: Callable[[dict[str, Any]], bool] | None = None, sender: str | None = None
) -> SigningAccount:
"""
Tracks and returns an Algorand account with private key loaded from the given KMD wallet.
:param name: The name of the wallet to retrieve an account from
:param predicate: Optional filter to use to find the account
:param sender: Optional sender address to use this signer for (aka a rekeyed account)
:returns: The account
:raises ValueError: If unable to find KMD account with given name and predicate
:example:
>>> # Get default funded account in a LocalNet:
>>> defaultDispenserAccount = account.from_kmd('unencrypted-default-wallet',
... lambda a: a.status != 'Offline' and a.amount > 1_000_000_000
... )
"""
kmd_account = self._kmd_account_manager.get_wallet_account(name, predicate, sender)
if not kmd_account:
raise ValueError(f"Unable to find KMD account {name}{' with predicate' if predicate else ''}")
return self._register_account(kmd_account.private_key)
def logicsig(self, program: bytes, args: list[bytes] | None = None) -> LogicSigAccount:
"""
Tracks and returns an account that represents a logic signature.
:param program: The bytes that make up the compiled logic signature
:param args: Optional (binary) arguments to pass into the logic signature
:returns: A logic signature account wrapper
:example:
>>> account = account.logic_sig(program, [new Uint8Array(3, ...)])
"""
return self._register_logicsig(program, args)
def multisig(self, metadata: MultisigMetadata, signing_accounts: list[SigningAccount]) -> MultiSigAccount:
"""
Tracks and returns an account that supports partial or full multisig signing.
:param metadata: The metadata for the multisig account
:param signing_accounts: The signers that are currently present
:returns: A multisig account wrapper
:example:
>>> account = account_manager.multi_sig(
... version=1,
... threshold=1,
... addrs=["ADDRESS1...", "ADDRESS2..."],
... signing_accounts=[account1, account2]
... )
"""
return self._register_multisig(metadata, signing_accounts)
def random(self) -> SigningAccount:
"""
Tracks and returns a new, random Algorand account.
:returns: The account
:example:
>>> account = account_manager.random()
"""
private_key, _ = algosdk.account.generate_account()
return self._register_account(private_key)
def localnet_dispenser(self) -> SigningAccount:
"""
Returns an Algorand account with private key loaded for the default LocalNet dispenser account.
This account can be used to fund other accounts.
:returns: The account
:example:
>>> account = account_manager.localnet_dispenser()
"""
kmd_account = self._kmd_account_manager.get_localnet_dispenser_account()
return self._register_account(kmd_account.private_key)
def dispenser_from_environment(self) -> SigningAccount:
"""
Returns an account (with private key loaded) that can act as a dispenser from environment variables.
If environment variables are not present, returns the default LocalNet dispenser account.
:returns: The account
:example:
>>> account = account_manager.dispenser_from_environment()
"""
name = os.getenv(f"{DISPENSER_ACCOUNT_NAME}_MNEMONIC")
if name:
return self.from_environment(DISPENSER_ACCOUNT_NAME)
return self.localnet_dispenser()
def rekeyed(
self, *, sender: str, account: TransactionSignerAccountProtocol
) -> TransactionSignerAccount | SigningAccount:
"""
Tracks and returns an Algorand account that is a rekeyed version of the given account to a new sender.
:param sender: The account or address to use as the sender
:param account: The account to use as the signer for this new rekeyed account
:returns: The rekeyed account
:example:
>>> account = account.from_mnemonic("mnemonic secret ...")
>>> rekeyed_account = account_manager.rekeyed(account, "SENDERADDRESS...")
"""
sender_address = sender.address if isinstance(sender, SigningAccount) else sender
self._accounts[sender_address] = TransactionSignerAccount(address=sender_address, signer=account.signer)
if isinstance(account, SigningAccount):
return SigningAccount(address=sender_address, private_key=account.private_key)
return TransactionSignerAccount(address=sender_address, signer=account.signer)
def rekey_account( # noqa: PLR0913
self,
account: str,
rekey_to: str | TransactionSignerAccountProtocol,
*, # Common transaction parameters
signer: TransactionSigner | None = None,
note: bytes | None = None,
lease: bytes | None = None,
static_fee: AlgoAmount | None = None,
extra_fee: AlgoAmount | None = None,
max_fee: AlgoAmount | None = None,
validity_window: int | None = None,
first_valid_round: int | None = None,
last_valid_round: int | None = None,
suppress_log: bool | None = None,
) -> SendAtomicTransactionComposerResults:
"""
Rekey an account to a new address.
:param account: The account to rekey
:param rekey_to: The address or account to rekey to
:param signer: Optional transaction signer
:param note: Optional transaction note
:param lease: Optional transaction lease
:param static_fee: Optional static fee
:param extra_fee: Optional extra fee
:param max_fee: Optional max fee
:param validity_window: Optional validity window
:param first_valid_round: Optional first valid round
:param last_valid_round: Optional last valid round
:param suppress_log: Optional flag to suppress logging
:returns: The result of the transaction and the transaction that was sent
.. warning::
Please be careful with this function and be sure to read the
`official rekey guidance <https://developer.algorand.org/docs/get-details/accounts/rekey/>`_.
:example:
>>> # Basic example (with string addresses):
>>> algorand.account.rekey_account({account: "ACCOUNTADDRESS", rekey_to: "NEWADDRESS"})
>>> # Basic example (with signer accounts):
>>> algorand.account.rekey_account({account: account1, rekey_to: newSignerAccount})
>>> # Advanced example:
>>> algorand.account.rekey_account({
... account: "ACCOUNTADDRESS",
... rekey_to: "NEWADDRESS",
... lease: 'lease',
... note: 'note',
... first_valid_round: 1000,
... validity_window: 10,
... extra_fee: AlgoAmount.from_micro_algo(1000),
... static_fee: AlgoAmount.from_micro_algo(1000),
... max_fee: AlgoAmount.from_micro_algo(3000),
... suppress_log: True,
... })
"""
sender_address = self._get_address(account)
rekey_address = self._get_address(rekey_to)
result = (
self._get_composer()
.add_payment(
PaymentParams(
sender=sender_address,
receiver=sender_address,
amount=AlgoAmount.from_micro_algo(0),
rekey_to=rekey_address,
signer=signer,
note=note,
lease=lease,
static_fee=static_fee,
extra_fee=extra_fee,
max_fee=max_fee,
validity_window=validity_window,
first_valid_round=first_valid_round,
last_valid_round=last_valid_round,
)
)
.send()
)
# If rekey_to is a signing account, set it as the signer for this account
if isinstance(rekey_to, SigningAccount):
self.rekeyed(sender=account, account=rekey_to)
if not suppress_log:
logger.info(f"Rekeyed {sender_address} to {rekey_address} via transaction {result.tx_ids[-1]}")
return result
def ensure_funded( # noqa: PLR0913
self,
account_to_fund: str | SigningAccount,
dispenser_account: str | SigningAccount,
min_spending_balance: AlgoAmount,
min_funding_increment: AlgoAmount | None = None,
# Sender params
send_params: SendParams | None = None,
# Common txn params
signer: TransactionSigner | None = None,
rekey_to: str | None = None,
note: bytes | None = None,
lease: bytes | None = None,
static_fee: AlgoAmount | None = None,
extra_fee: AlgoAmount | None = None,
max_fee: AlgoAmount | None = None,
validity_window: int | None = None,
first_valid_round: int | None = None,
last_valid_round: int | None = None,
) -> EnsureFundedResult | None:
"""
Funds a given account using a dispenser account as a funding source.
Ensures the given account has a certain amount of Algo free to spend (accounting for
Algo locked in minimum balance requirement).
See `<https://developer.algorand.org/docs/get-details/accounts/#minimum-balance>`_ for details.
:param account_to_fund: The account to fund
:param dispenser_account: The account to use as a dispenser funding source
:param min_spending_balance: The minimum balance of Algo that the account
should have available to spend
:param min_funding_increment: Optional minimum funding increment
:param send_params: Parameters for the send operation, defaults to None
:param signer: Optional transaction signer
:param rekey_to: Optional rekey address
:param note: Optional transaction note
:param lease: Optional transaction lease
:param static_fee: Optional static fee
:param extra_fee: Optional extra fee
:param max_fee: Optional maximum fee
:param validity_window: Optional validity window
:param first_valid_round: Optional first valid round
:param last_valid_round: Optional last valid round
:returns: The result of executing the dispensing transaction and the `amountFunded` if funds were needed,
or None if no funds were needed
:example:
>>> # Basic example:
>>> algorand.account.ensure_funded("ACCOUNTADDRESS", "DISPENSERADDRESS", algokit.algo(1))
>>> # With configuration:
>>> algorand.account.ensure_funded(
... "ACCOUNTADDRESS",
... "DISPENSERADDRESS",
... algokit.algo(1),
... min_funding_increment=algokit.algo(2),
... fee=AlgoAmount.from_micro_algo(1000),
... suppress_log=True
... )
"""
account_to_fund = self._get_address(account_to_fund)
dispenser_account = self._get_address(dispenser_account)
amount_funded = self._get_ensure_funded_amount(account_to_fund, min_spending_balance, min_funding_increment)
if not amount_funded:
return None
result = (
self._get_composer()
.add_payment(
PaymentParams(
sender=dispenser_account,
receiver=account_to_fund,
amount=amount_funded,
signer=signer,
rekey_to=rekey_to,
note=note,
lease=lease,
static_fee=static_fee,
extra_fee=extra_fee,
max_fee=max_fee,
validity_window=validity_window,
first_valid_round=first_valid_round,
last_valid_round=last_valid_round,
)
)
.send(send_params)
)
return EnsureFundedResult(
returns=result.returns,
transactions=result.transactions,
confirmations=result.confirmations,
tx_ids=result.tx_ids,
group_id=result.group_id,
transaction_id=result.tx_ids[0],
confirmation=result.confirmations[0],
transaction=result.transactions[0],
amount_funded=amount_funded,
)
def ensure_funded_from_environment( # noqa: PLR0913
self,
account_to_fund: str | SigningAccount,
min_spending_balance: AlgoAmount,
*, # Force remaining params to be keyword-only
min_funding_increment: AlgoAmount | None = None,
# SendParams
send_params: SendParams | None = None,
# Common transaction params (omitting sender)
signer: TransactionSigner | None = None,
rekey_to: str | None = None,
note: bytes | None = None,
lease: bytes | None = None,
static_fee: AlgoAmount | None = None,
extra_fee: AlgoAmount | None = None,
max_fee: AlgoAmount | None = None,
validity_window: int | None = None,
first_valid_round: int | None = None,
last_valid_round: int | None = None,
) -> EnsureFundedResult | None:
"""
Ensure an account is funded from a dispenser account configured in environment.
Uses a dispenser account retrieved from the environment, per the `dispenser_from_environment` method,
as a funding source such that the given account has a certain amount of Algo free to spend
(accounting for Algo locked in minimum balance requirement).
See `<https://developer.algorand.org/docs/get-details/accounts/#minimum-balance>`_ for details.
:param account_to_fund: The account to fund
:param min_spending_balance: The minimum balance of Algo that the account should have available to
spend
:param min_funding_increment: Optional minimum funding increment
:param send_params: Parameters for the send operation, defaults to None
:param signer: Optional transaction signer
:param rekey_to: Optional rekey address
:param note: Optional transaction note
:param lease: Optional transaction lease
:param static_fee: Optional static fee
:param extra_fee: Optional extra fee
:param max_fee: Optional maximum fee
:param validity_window: Optional validity window
:param first_valid_round: Optional first valid round
:param last_valid_round: Optional last valid round
:returns: The result of executing the dispensing transaction and the `amountFunded` if funds were needed, or
None if no funds were needed
.. note::
The dispenser account is retrieved from the account mnemonic stored in
process.env.DISPENSER_MNEMONIC and optionally process.env.DISPENSER_SENDER
if it's a rekeyed account, or against default LocalNet if no environment variables present.
:example:
>>> # Basic example:
>>> algorand.account.ensure_funded_from_environment("ACCOUNTADDRESS", algokit.algo(1))
>>> # With configuration:
>>> algorand.account.ensure_funded_from_environment(
... "ACCOUNTADDRESS",
... algokit.algo(1),
... min_funding_increment=algokit.algo(2),
... fee=AlgoAmount.from_micro_algo(1000),
... suppress_log=True
... )
"""
account_to_fund = self._get_address(account_to_fund)
dispenser_account = self.dispenser_from_environment()
amount_funded = self._get_ensure_funded_amount(account_to_fund, min_spending_balance, min_funding_increment)
if not amount_funded:
return None
result = (
self._get_composer()
.add_payment(
PaymentParams(
sender=dispenser_account.address,
receiver=account_to_fund,
amount=amount_funded,
signer=signer,
rekey_to=rekey_to,
note=note,
lease=lease,
static_fee=static_fee,
extra_fee=extra_fee,
max_fee=max_fee,
validity_window=validity_window,
first_valid_round=first_valid_round,
last_valid_round=last_valid_round,
)
)
.send(send_params)
)
return EnsureFundedResult(
returns=result.returns,
transactions=result.transactions,
confirmations=result.confirmations,
tx_ids=result.tx_ids,
group_id=result.group_id,
transaction_id=result.tx_ids[0],
confirmation=result.confirmations[0],
transaction=result.transactions[0],
amount_funded=amount_funded,
)
def ensure_funded_from_testnet_dispenser_api(
self,
account_to_fund: str | SigningAccount,
dispenser_client: TestNetDispenserApiClient,
min_spending_balance: AlgoAmount,
*,
min_funding_increment: AlgoAmount | None = None,
) -> EnsureFundedFromTestnetDispenserApiResult | None:
"""
Ensure an account is funded using the TestNet Dispenser API.
Uses the TestNet Dispenser API as a funding source such that the account has a certain amount
of Algo free to spend (accounting for Algo locked in minimum balance requirement).
See `<https://developer.algorand.org/docs/get-details/accounts/#minimum-balance>`_ for details.
:param account_to_fund: The account to fund
:param dispenser_client: The TestNet dispenser funding client
:param min_spending_balance: The minimum balance of Algo that the account should have
available to spend
:param min_funding_increment: Optional minimum funding increment
:returns: The result of executing the dispensing transaction and the `amountFunded` if funds were needed, or
None if no funds were needed
:raises ValueError: If attempting to fund on non-TestNet network
:example:
>>> # Basic example:
>>> algorand.account.ensure_funded_from_testnet_dispenser_api(
... "ACCOUNTADDRESS",
... algorand.client.get_testnet_dispenser_from_environment(),
... algokit.algo(1)
... )
>>> # With configuration:
>>> algorand.account.ensure_funded_from_testnet_dispenser_api(
... "ACCOUNTADDRESS",
... algorand.client.get_testnet_dispenser_from_environment(),
... algokit.algo(1),
... min_funding_increment=algokit.algo(2)
... )
"""
account_to_fund = self._get_address(account_to_fund)
if not self._client_manager.is_testnet():
raise ValueError("Attempt to fund using TestNet dispenser API on non TestNet network.")
amount_funded = self._get_ensure_funded_amount(account_to_fund, min_spending_balance, min_funding_increment)
if not amount_funded:
return None
result = dispenser_client.fund(
address=account_to_fund,
amount=amount_funded.micro_algo,
asset_id=DispenserAssetName.ALGO,
)
return EnsureFundedFromTestnetDispenserApiResult(
transaction_id=result.tx_id,
amount_funded=AlgoAmount.from_micro_algo(result.amount),
)
def _get_address(self, sender: str | TransactionSignerAccountProtocol) -> str:
match sender:
case TransactionSignerAccountProtocol():
return sender.address
case str():
return sender
case _:
raise ValueError(f"Unknown sender type: {type(sender)}")
def _get_composer(self, get_suggested_params: Callable[[], SuggestedParams] | None = None) -> TransactionComposer:
if get_suggested_params is None:
def _get_suggested_params() -> SuggestedParams:
return self._client_manager.algod.suggested_params()
get_suggested_params = _get_suggested_params
return TransactionComposer(
algod=self._client_manager.algod, get_signer=self.get_signer, get_suggested_params=get_suggested_params
)
def _calculate_fund_amount(
self,
min_spending_balance: int,
current_spending_balance: AlgoAmount,
min_funding_increment: int,
) -> int | None:
if min_spending_balance > current_spending_balance:
min_fund_amount = (min_spending_balance - current_spending_balance).micro_algo
return max(min_fund_amount, min_funding_increment)
return None
def _get_ensure_funded_amount(
self,
sender: str,
min_spending_balance: AlgoAmount,
min_funding_increment: AlgoAmount | None = None,
) -> AlgoAmount | None:
account_info = self.get_information(sender)
current_spending_balance = account_info.amount - account_info.min_balance
min_increment = min_funding_increment.micro_algo if min_funding_increment else 0
amount_funded = self._calculate_fund_amount(
min_spending_balance.micro_algo, current_spending_balance, min_increment
)
return AlgoAmount.from_micro_algo(amount_funded) if amount_funded is not None else None