-
Notifications
You must be signed in to change notification settings - Fork 11
/
algorand_client.py
319 lines (272 loc) · 12.4 KB
/
algorand_client.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
import copy
import time
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any
from algokit_utils.beta.account_manager import AccountManager
from algokit_utils.beta.client_manager import AlgoSdkClients, ClientManager
from algokit_utils.beta.composer import (
AlgokitComposer,
AppCallParams,
AssetConfigParams,
AssetCreateParams,
AssetDestroyParams,
AssetFreezeParams,
AssetOptInParams,
AssetTransferParams,
MethodCallParams,
OnlineKeyRegParams,
PayParams,
)
from algokit_utils.network_clients import (
AlgoClientConfigs,
get_algod_client,
get_algonode_config,
get_default_localnet_config,
get_indexer_client,
get_kmd_client,
)
from algosdk.atomic_transaction_composer import AtomicTransactionResponse, TransactionSigner
from algosdk.transaction import SuggestedParams, Transaction, wait_for_confirmation
from typing_extensions import Self
__all__ = [
"AlgorandClient",
"AssetCreateParams",
"AssetOptInParams",
"MethodCallParams",
"PayParams",
"AssetFreezeParams",
"AssetConfigParams",
"AssetDestroyParams",
"AppCallParams",
"OnlineKeyRegParams",
"AssetTransferParams",
]
@dataclass
class AlgorandClientSendMethods:
"""
Methods used to send a transaction to the network and wait for confirmation
"""
payment: Callable[[PayParams], dict[str, Any]]
asset_create: Callable[[AssetCreateParams], dict[str, Any]]
asset_config: Callable[[AssetConfigParams], dict[str, Any]]
asset_freeze: Callable[[AssetFreezeParams], dict[str, Any]]
asset_destroy: Callable[[AssetDestroyParams], dict[str, Any]]
asset_transfer: Callable[[AssetTransferParams], dict[str, Any]]
app_call: Callable[[AppCallParams], dict[str, Any]]
online_key_reg: Callable[[OnlineKeyRegParams], dict[str, Any]]
method_call: Callable[[MethodCallParams], dict[str, Any]]
asset_opt_in: Callable[[AssetOptInParams], dict[str, Any]]
@dataclass
class AlgorandClientTransactionMethods:
"""
Methods used to form a transaction without signing or sending to the network
"""
payment: Callable[[PayParams], Transaction]
asset_create: Callable[[AssetCreateParams], Transaction]
asset_config: Callable[[AssetConfigParams], Transaction]
asset_freeze: Callable[[AssetFreezeParams], Transaction]
asset_destroy: Callable[[AssetDestroyParams], Transaction]
asset_transfer: Callable[[AssetTransferParams], Transaction]
app_call: Callable[[AppCallParams], Transaction]
online_key_reg: Callable[[OnlineKeyRegParams], Transaction]
method_call: Callable[[MethodCallParams], list[Transaction]]
asset_opt_in: Callable[[AssetOptInParams], Transaction]
class AlgorandClient:
"""A client that brokers easy access to Algorand functionality."""
def __init__(self, config: AlgoClientConfigs | AlgoSdkClients):
self._client_manager: ClientManager = ClientManager(config)
self._account_manager: AccountManager = AccountManager(self._client_manager)
self._cached_suggested_params: SuggestedParams | None = None
self._cached_suggested_params_expiry: float | None = None
self._cached_suggested_params_timeout: int = 3_000 # three seconds
self._default_validity_window: int = 10
def _unwrap_single_send_result(self, results: AtomicTransactionResponse) -> dict[str, Any]:
return {
"confirmation": wait_for_confirmation(self._client_manager.algod, results.tx_ids[0]),
"tx_id": results.tx_ids[0],
}
def set_default_validity_window(self, validity_window: int) -> Self:
"""
Sets the default validity window for transactions.
:param validity_window: The number of rounds between the first and last valid rounds
:return: The `AlgorandClient` so method calls can be chained
"""
self._default_validity_window = validity_window
return self
def set_default_signer(self, signer: TransactionSigner) -> Self:
"""
Sets the default signer to use if no other signer is specified.
:param signer: The signer to use, either a `TransactionSigner` or a `TransactionSignerAccount`
:return: The `AlgorandClient` so method calls can be chained
"""
self._account_manager.set_default_signer(signer)
return self
def set_signer(self, sender: str, signer: TransactionSigner) -> Self:
"""
Tracks the given account for later signing.
:param sender: The sender address to use this signer for
:param signer: The signer to sign transactions with for the given sender
:return: The `AlgorandClient` so method calls can be chained
"""
self._account_manager.set_signer(sender, signer)
return self
def set_suggested_params(self, suggested_params: SuggestedParams, until: float | None = None) -> Self:
"""
Sets a cache value to use for suggested params.
:param suggested_params: The suggested params to use
:param until: A timestamp until which to cache, or if not specified then the timeout is used
:return: The `AlgorandClient` so method calls can be chained
"""
self._cached_suggested_params = suggested_params
self._cached_suggested_params_expiry = until or time.time() + self._cached_suggested_params_timeout
return self
def set_suggested_params_timeout(self, timeout: int) -> Self:
"""
Sets the timeout for caching suggested params.
:param timeout: The timeout in milliseconds
:return: The `AlgorandClient` so method calls can be chained
"""
self._cached_suggested_params_timeout = timeout
return self
def get_suggested_params(self) -> SuggestedParams:
"""Get suggested params for a transaction (either cached or from algod if the cache is stale or empty)"""
if self._cached_suggested_params and (
self._cached_suggested_params_expiry is None or self._cached_suggested_params_expiry > time.time()
):
return copy.deepcopy(self._cached_suggested_params)
self._cached_suggested_params = self._client_manager.algod.suggested_params()
self._cached_suggested_params_expiry = time.time() + self._cached_suggested_params_timeout
return copy.deepcopy(self._cached_suggested_params)
@property
def client(self) -> ClientManager:
"""Get clients, including algosdk clients and app clients."""
return self._client_manager
@property
def account(self) -> AccountManager:
"""Get or create accounts that can sign transactions."""
return self._account_manager
def new_group(self) -> AlgokitComposer:
"""Start a new `AlgokitComposer` transaction group"""
return AlgokitComposer(
algod=self.client.algod,
get_signer=lambda addr: self.account.get_signer(addr),
get_suggested_params=self.get_suggested_params,
default_validity_window=self._default_validity_window,
)
@property
def send(self) -> AlgorandClientSendMethods:
"""Methods for sending a transaction and waiting for confirmation"""
return AlgorandClientSendMethods(
payment=lambda params: self._unwrap_single_send_result(self.new_group().add_payment(params).execute()),
asset_create=lambda params: self._unwrap_single_send_result(
self.new_group().add_asset_create(params).execute()
),
asset_config=lambda params: self._unwrap_single_send_result(
self.new_group().add_asset_config(params).execute()
),
asset_freeze=lambda params: self._unwrap_single_send_result(
self.new_group().add_asset_freeze(params).execute()
),
asset_destroy=lambda params: self._unwrap_single_send_result(
self.new_group().add_asset_destroy(params).execute()
),
asset_transfer=lambda params: self._unwrap_single_send_result(
self.new_group().add_asset_transfer(params).execute()
),
app_call=lambda params: self._unwrap_single_send_result(self.new_group().add_app_call(params).execute()),
online_key_reg=lambda params: self._unwrap_single_send_result(
self.new_group().add_online_key_reg(params).execute()
),
method_call=lambda params: self._unwrap_single_send_result(
self.new_group().add_method_call(params).execute()
),
asset_opt_in=lambda params: self._unwrap_single_send_result(
self.new_group().add_asset_opt_in(params).execute()
),
)
@property
def transactions(self) -> AlgorandClientTransactionMethods:
"""Methods for building transactions"""
return AlgorandClientTransactionMethods(
payment=lambda params: self.new_group().add_payment(params).build_group()[0].txn,
asset_create=lambda params: self.new_group().add_asset_create(params).build_group()[0].txn,
asset_config=lambda params: self.new_group().add_asset_config(params).build_group()[0].txn,
asset_freeze=lambda params: self.new_group().add_asset_freeze(params).build_group()[0].txn,
asset_destroy=lambda params: self.new_group().add_asset_destroy(params).build_group()[0].txn,
asset_transfer=lambda params: self.new_group().add_asset_transfer(params).build_group()[0].txn,
app_call=lambda params: self.new_group().add_app_call(params).build_group()[0].txn,
online_key_reg=lambda params: self.new_group().add_online_key_reg(params).build_group()[0].txn,
method_call=lambda params: [txn.txn for txn in self.new_group().add_method_call(params).build_group()],
asset_opt_in=lambda params: self.new_group().add_asset_opt_in(params).build_group()[0].txn,
)
@staticmethod
def default_local_net() -> "AlgorandClient":
"""
Returns an `AlgorandClient` pointing at default LocalNet ports and API token.
:return: The `AlgorandClient`
"""
return AlgorandClient(
AlgoClientConfigs(
algod_config=get_default_localnet_config("algod"),
indexer_config=get_default_localnet_config("indexer"),
kmd_config=get_default_localnet_config("kmd"),
)
)
@staticmethod
def test_net() -> "AlgorandClient":
"""
Returns an `AlgorandClient` pointing at TestNet using AlgoNode.
:return: The `AlgorandClient`
"""
return AlgorandClient(
AlgoClientConfigs(
algod_config=get_algonode_config("testnet", "algod", ""),
indexer_config=get_algonode_config("testnet", "indexer", ""),
kmd_config=None,
)
)
@staticmethod
def main_net() -> "AlgorandClient":
"""
Returns an `AlgorandClient` pointing at MainNet using AlgoNode.
:return: The `AlgorandClient`
"""
return AlgorandClient(
AlgoClientConfigs(
algod_config=get_algonode_config("mainnet", "algod", ""),
indexer_config=get_algonode_config("mainnet", "indexer", ""),
kmd_config=None,
)
)
@staticmethod
def from_clients(clients: AlgoSdkClients) -> "AlgorandClient":
"""
Returns an `AlgorandClient` pointing to the given client(s).
:param clients: The clients to use
:return: The `AlgorandClient`
"""
return AlgorandClient(clients)
@staticmethod
def from_environment() -> "AlgorandClient":
"""
Returns an `AlgorandClient` loading the configuration from environment variables.
Retrieve configurations from environment variables when defined or get defaults.
Expects to be called from a Python environment.
:return: The `AlgorandClient`
"""
return AlgorandClient(
AlgoSdkClients(
algod=get_algod_client(),
kmd=get_kmd_client(),
indexer=get_indexer_client(),
)
)
@staticmethod
def from_config(config: AlgoClientConfigs) -> "AlgorandClient":
"""
Returns an `AlgorandClient` from the given config.
:param config: The config to use
:return: The `AlgorandClient`
"""
return AlgorandClient(config)