-
Notifications
You must be signed in to change notification settings - Fork 3
/
smart_asa_client.py
365 lines (320 loc) · 10.9 KB
/
smart_asa_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
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
"""
Smart ASA client
"""
__author__ = "Cosimo Bassi, Stefano De Angelis"
__email__ = "<[email protected]>, <[email protected]>"
from typing import Any, Optional, Union
from algosdk.abi import Contract
from algosdk.atomic_transaction_composer import TransactionWithSigner
from algosdk.v2client.algod import AlgodClient
from algosdk.encoding import encode_address
from algosdk.future.transaction import AssetTransferTxn, OnComplete
from account import Account, AppAccount
from utils import get_params, normalize_getter_params
from smart_asa_asc import (
SMART_ASA_APP_BINDING,
UNDERLYING_ASA_TOTAL,
GlobalState,
LocalState,
)
def get_smart_asa_params(algod_client: AlgodClient, smart_asa_id: int) -> dict:
smart_asa = algod_client.asset_info(smart_asa_id)["params"]
assert SMART_ASA_APP_BINDING in smart_asa["url"]
smart_asa_app_id = int(smart_asa["url"].replace(SMART_ASA_APP_BINDING, ""))
smart_asa_app_account = AppAccount.from_app_id(
app_id=smart_asa_app_id,
algod_client=algod_client,
)
smart_asa_state = smart_asa_app_account.global_state()
smart_asa_app = algod_client.application_info(smart_asa_app_id)["params"]
circulating_supply = UNDERLYING_ASA_TOTAL.value - smart_asa_app_account.asa_balance(
smart_asa_id
)
return {
"smart_asa_id": smart_asa_id,
"app_id": smart_asa_app_id,
"app_address": smart_asa_app_account.address,
"creator_addr": smart_asa_app["creator"],
"circulating_supply": circulating_supply,
"unit_name": smart_asa_state["unit_name"].decode(),
"name": smart_asa_state["name"].decode(),
"url": smart_asa_state["url"].decode(),
"metadata_hash": smart_asa_state["metadata_hash"],
"total": int(smart_asa_state["total"]),
"decimals": int(smart_asa_state["decimals"]),
"frozen": bool(smart_asa_state["frozen"]),
"default_frozen": bool(smart_asa_state["default_frozen"]),
"manager_addr": encode_address(smart_asa_state["manager_addr"]),
"reserve_addr": encode_address(smart_asa_state["reserve_addr"]),
"freeze_addr": encode_address(smart_asa_state["freeze_addr"]),
"clawback_addr": encode_address(smart_asa_state["clawback_addr"]),
}
def smart_asa_app_create(
teal_approval: str, teal_clear: str, creator: Account
) -> AppAccount:
return creator.create_asc(
approval_program=teal_approval,
clear_program=teal_clear,
global_schema=GlobalState.schema(),
local_schema=LocalState.schema(),
)
def smart_asa_create(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
creator: Account,
total: int,
decimals: int = 0,
default_frozen: bool = False,
unit_name: str = "",
name: str = "",
url: str = "",
metadata_hash: bytes = b"",
manager_addr: Optional[Union[str, Account]] = None,
reserve_addr: Optional[Union[str, Account]] = None,
freeze_addr: Optional[Union[str, Account]] = None,
clawback_addr: Optional[Union[str, Account]] = None,
save_abi_call: Optional[str] = None,
) -> int:
params = get_params(creator.algod_client)
abi_call_fee = params.fee * 2
return creator.abi_call(
smart_asa_contract.get_method_by_name("asset_create"),
total,
decimals,
default_frozen,
unit_name,
name,
url,
metadata_hash,
manager_addr if manager_addr is not None else creator,
reserve_addr if reserve_addr is not None else creator,
freeze_addr if freeze_addr is not None else creator,
clawback_addr if clawback_addr is not None else creator,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
def smart_asa_optin(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
asset_id: int,
caller: Account,
debug_txn: Optional[TransactionWithSigner] = None,
save_abi_call: Optional[str] = None,
) -> None:
params = get_params(caller.algod_client)
abi_call_fee = params.fee
if debug_txn:
asa_optin_txn = debug_txn
else:
asa_optin_txn = AssetTransferTxn(
sender=caller.address,
sp=params,
receiver=caller.address,
amt=0,
index=asset_id,
)
asa_optin_txn = TransactionWithSigner(
txn=asa_optin_txn,
signer=caller,
)
caller.abi_call(
smart_asa_contract.get_method_by_name("asset_app_optin"),
asset_id,
asa_optin_txn,
on_complete=OnComplete.OptInOC,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
def smart_asa_closeout(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
asset_id: int,
caller: Account,
close_to: Union[str, Account],
debug_txn: Optional[TransactionWithSigner] = None,
save_abi_call: Optional[str] = None,
) -> None:
params = get_params(caller.algod_client)
abi_call_fee = params.fee * 2
if debug_txn:
asa_close_to_txn = debug_txn
else:
asa_close_to_txn = AssetTransferTxn(
sender=caller.address,
sp=params,
receiver=caller.address,
amt=0,
index=asset_id,
close_assets_to=smart_asa_app.address,
)
asa_close_to_txn = TransactionWithSigner(
txn=asa_close_to_txn,
signer=caller,
)
caller.abi_call(
smart_asa_contract.get_method_by_name("asset_app_closeout"),
asset_id,
close_to,
on_complete=OnComplete.CloseOutOC,
app=smart_asa_app,
fee=abi_call_fee,
group_extra_txns=[asa_close_to_txn],
save_abi_call=save_abi_call,
)
def smart_asa_config(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
manager: Account,
asset_id: int,
config_total: Optional[int] = None,
config_decimals: Optional[int] = None,
config_default_frozen: Optional[bool] = None,
config_unit_name: Optional[str] = None,
config_name: Optional[str] = None,
config_url: Optional[str] = None,
config_metadata_hash: Optional[bytes] = None,
config_manager_addr: Optional[Union[str, Account]] = None,
config_reserve_addr: Optional[Union[str, Account]] = None,
config_freeze_addr: Optional[Union[str, Account]] = None,
config_clawback_addr: Optional[Union[str, Account]] = None,
save_abi_call: Optional[str] = None,
) -> int:
s_asa = get_smart_asa_params(manager.algod_client, asset_id)
if config_metadata_hash is None:
smart_asa_params = normalize_getter_params(
smart_asa_get(
smart_asa_contract=smart_asa_contract,
smart_asa_app=smart_asa_app,
caller=manager,
asset_id=asset_id,
getter="get_asset_config",
)
)
config_metadata_hash = bytes(smart_asa_params.metadata_hash)
if config_manager_addr is None:
config_manager_addr = Account(address=s_asa["manager_addr"])
if config_reserve_addr is None:
config_reserve_addr = Account(address=s_asa["reserve_addr"])
if config_freeze_addr is None:
config_freeze_addr = Account(address=s_asa["freeze_addr"])
if config_clawback_addr is None:
config_clawback_addr = Account(address=s_asa["clawback_addr"])
params = get_params(manager.algod_client)
abi_call_fee = params.fee * 2
manager.abi_call(
smart_asa_contract.get_method_by_name("asset_config"),
asset_id,
s_asa["total"] if config_total is None else config_total,
s_asa["decimals"] if config_decimals is None else config_decimals,
s_asa["default_frozen"]
if config_default_frozen is None
else config_default_frozen,
s_asa["unit_name"] if config_unit_name is None else config_unit_name,
s_asa["name"] if config_name is None else config_name,
s_asa["url"] if config_url is None else config_url,
config_metadata_hash,
config_manager_addr,
config_reserve_addr,
config_freeze_addr,
config_clawback_addr,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
return asset_id
def smart_asa_transfer(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
xfer_asset: int,
asset_amount: int,
caller: Account,
asset_receiver: Account,
asset_sender: Optional[Union[str, Account]] = None,
save_abi_call: Optional[str] = None,
) -> None:
params = get_params(caller.algod_client)
abi_call_fee = params.fee * 2
caller.abi_call(
smart_asa_contract.get_method_by_name("asset_transfer"),
xfer_asset,
asset_amount,
caller if asset_sender is None else asset_sender,
asset_receiver,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
def smart_asa_freeze(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
freezer: Account,
freeze_asset: int,
asset_frozen: bool = False,
save_abi_call: Optional[str] = None,
) -> None:
params = get_params(freezer.algod_client)
abi_call_fee = params.fee * 2
freezer.abi_call(
smart_asa_contract.get_method_by_name("asset_freeze"),
freeze_asset,
asset_frozen,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
def smart_asa_account_freeze(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
freezer: Account,
freeze_asset: int,
target_account: Account,
account_frozen: bool = False,
save_abi_call: Optional[str] = None,
) -> None:
params = get_params(freezer.algod_client)
abi_call_fee = params.fee * 2
freezer.abi_call(
smart_asa_contract.get_method_by_name("account_freeze"),
freeze_asset,
target_account,
account_frozen,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
def smart_asa_destroy(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
manager: Account,
destroy_asset: int,
save_abi_call: Optional[str] = None,
) -> None:
params = get_params(manager.algod_client)
abi_call_fee = params.fee * 2
manager.abi_call(
smart_asa_contract.get_method_by_name("asset_destroy"),
destroy_asset,
app=smart_asa_app,
fee=abi_call_fee,
save_abi_call=save_abi_call,
)
def smart_asa_get(
smart_asa_contract: Contract,
smart_asa_app: AppAccount,
caller: Account,
asset_id: int,
getter: str,
account: Optional[Union[str, Account]] = None,
save_abi_call: Optional[str] = None,
) -> Any:
args = [asset_id]
if account is not None:
args.append(account)
return caller.abi_call(
smart_asa_contract.get_method_by_name(getter),
*args,
app=smart_asa_app,
save_abi_call=save_abi_call,
)