-
Notifications
You must be signed in to change notification settings - Fork 2
/
deploy.py
402 lines (308 loc) · 12.1 KB
/
deploy.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import annotations
import base64
import dataclasses
import json
from typing import Any, Optional, Union, cast
import algosdk
from algosdk.abi import Method
from algosdk import encoding, mnemonic, util
from algosdk.atomic_transaction_composer import (
AtomicTransactionComposer,
TransactionSigner,
)
from algosdk.future import transaction
from algosdk.kmd import KMDClient
from algosdk.v2client import algod
from algosdk.wallet import Wallet
from state import AVMState
from tc_asa import (
ABI,
Config,
LocalConfig,
asc_approval,
compile_stateful,
init,
on_closeout_or_clear,
)
ALGOD_ADDRESS = "http://localhost:4001"
ALGOD_TOKEN = "a" * 64
KMD_ADDRESS = "http://localhost:4002"
KMD_TOKEN = ALGOD_TOKEN
ASSET_UNIT_NAME = "TC-ASA"
ASSET_NAME = "TCASA"
ASSET_TOTAL = int(2**64 - 1)
ASSET_DECIMALS = 6
FUND_ACCOUNT_ALGOS = util.algos_to_microalgos(100) # Algos
MIN_FLAT_FEE = util.algos_to_microalgos(0.001)
WITH_INNER_TXN_FEE = MIN_FLAT_FEE * 2
SC_MIN_BALANCE = util.algos_to_microalgos(0.1 * 2)
MAX_WAIT_ROUNDS = 10
algod_client = algod.AlgodClient(algod_token=ALGOD_TOKEN, algod_address=ALGOD_ADDRESS)
kmd_client = KMDClient(kmd_token=KMD_TOKEN, kmd_address=KMD_ADDRESS)
@dataclasses.dataclass(frozen=True)
class Account(TransactionSigner):
address: str
private_key: Optional[
str
] # Must be explicitly set to None when setting `lsig` or `app`.
lsig: Optional[transaction.LogicSig] = None
app: Optional[int] = None
def __post_init__(self):
assert self.private_key or self.lsig or self.app
def mnemonic(self) -> str:
return mnemonic.from_private_key(self.private_key)
def is_lsig(self) -> bool:
return bool(not self.private_key and self.lsig)
@classmethod
def create_account(cls) -> "Account":
private_key, address = algosdk.account.generate_account()
return cls(private_key=private_key, address=cast(str, address))
@property
def decoded_address(self):
return encoding.decode_address(self.address)
def sign_transactions(
self, txn_group: list[transaction.Transaction], indexes: list[int]
) -> list:
# Enables using `self` with `AtomicTransactionComposer`
for i in indexes:
txn_group[i] = sign(self, txn_group[i]) # type: ignore
return txn_group
def get_params(client: algod.AlgodClient, fee=None) -> transaction.SuggestedParams:
params = client.suggested_params()
params.flat_fee = True
params.fee = fee or MIN_FLAT_FEE
return params
def wait_for_confirmation(client, txid: str):
"""
Wait until the transaction is confirmed before proceeding.
"""
last_round = client.status().get("last-round")
txinfo = client.pending_transaction_info(txid)
while not txinfo.get("confirmed-round", -1) > 0:
print(f"Waiting for transaction {txid} confirmation.")
last_round += 1
client.status_after_block(last_round)
txinfo = client.pending_transaction_info(txid)
print(f"Transaction {txid} confirmed in round {txinfo.get('confirmed-round')}.")
return txinfo
def sign(account: Account, txn):
if account.is_lsig():
return transaction.LogicSigTransaction(txn, account.lsig) # type: ignore
assert account.private_key
return txn.sign(account.private_key)
def sign_send_wait(account: Account, txn):
"""Sign a transaction, submit it, and wait for its confirmation."""
signed_txn = sign(account, txn)
tx_id = signed_txn.transaction.get_txid()
transaction.write_to_file([signed_txn], "/tmp/txn.signed", overwrite=True)
algod_client.send_transactions([signed_txn])
wait_for_confirmation(algod_client, tx_id)
return algod_client.pending_transaction_info(tx_id)
def create_asset(creator_account: Account, **kwargs) -> int:
"""Create an asset and return its ID."""
params = get_params(algod_client)
config_kwargs = {
"sender": creator_account.address,
"sp": params,
"total": ASSET_TOTAL,
"default_frozen": True,
"unit_name": ASSET_UNIT_NAME,
"asset_name": ASSET_NAME,
"manager": creator_account.address,
"reserve": creator_account.address,
"freeze": creator_account.address,
"clawback": creator_account.address,
"decimals": ASSET_DECIMALS,
}
config_kwargs.update(**kwargs)
txn = transaction.AssetConfigTxn(**config_kwargs)
ptx = sign_send_wait(creator_account, txn)
return ptx["asset-index"]
def create_tc_asa(creator_account: Account, tc_asc_idx: int) -> int:
tc_asc_address = app_idx_to_account(tc_asc_idx).address
kwargs = {
"manager": tc_asc_address,
"reserve": tc_asc_address,
"freeze": tc_asc_address,
"clawback": tc_asc_address,
# TODO: Making up my own standard here :) standardize this.
"url": f"https://algorand.com/tc-asa/{tc_asc_idx}",
"unit_name": ASSET_UNIT_NAME,
"asset_name": ASSET_NAME,
"decimals": ASSET_DECIMALS,
}
return create_asset(creator_account, **kwargs)
def optin_to_asset(account: Account, asset_id: int):
params = get_params(algod_client)
txn = transaction.AssetTransferTxn(
sender=account.address,
sp=params,
receiver=account.address,
amt=0,
index=asset_id,
)
return sign_send_wait(account, txn)
def compile_program(source_code):
compile_response = algod_client.compile(source_code)
return base64.b64decode(compile_response["result"])
def optin_to_application(account: Account, app_id: int):
params = get_params(algod_client)
txn = transaction.ApplicationOptInTxn(account.address, params, app_id)
return sign_send_wait(account, txn)
def find_sandbox_faucet() -> Account:
default_wallet_name = kmd_client.list_wallets()[0]["name"]
wallet = Wallet(
default_wallet_name, "", kmd_client
) # Sandbox's wallet has no password
for account_ in wallet.list_keys():
info = algod_client.account_info(account_)
if (
info
and info.get("status") == "Online"
# and info.get("created-at-round", 0) == 0 # Needs the indexer.
):
return Account(address=account_, private_key=wallet.export_key(account_))
raise KeyError("Could not find sandbox faucet")
def create_and_fund(faucet: Account) -> Account:
new_account = Account.create_account()
print(f"Funding new account: {new_account.address}.")
fund(faucet, new_account)
return new_account
def fund(faucet: Account, receiver: Account, amount=FUND_ACCOUNT_ALGOS):
params = get_params(algod_client)
txn = transaction.PaymentTxn(faucet.address, params, receiver.address, amount)
return sign_send_wait(faucet, txn)
def app_idx_to_account(app_idx: int) -> Account:
return Account(
cast(
str,
encoding.encode_address(
encoding.checksum(b"appID" + (app_idx).to_bytes(8, "big"))
),
),
private_key=None,
app=app_idx,
)
def create_application(master: Account, cfg: Config) -> Account:
"""Deploy an ASC1 and return its index."""
global_schema = transaction.StateSchema(Config.n_uints(), Config.n_bytes())
local_schema = transaction.StateSchema(LocalConfig.n_uints(), LocalConfig.n_bytes())
approval_program = compile_program(compile_stateful(asc_approval(cfg)))
clear_program = compile_program(compile_stateful(on_closeout_or_clear(cfg)))
on_complete = transaction.OnComplete.NoOpOC.real
params = get_params(algod_client)
txn = transaction.ApplicationCreateTxn(
master.address,
params,
on_complete,
approval_program,
clear_program,
global_schema,
local_schema,
extra_pages=(len(approval_program) + len(clear_program)) // 2048,
)
transaction_response = sign_send_wait(master, txn)
asc_idx = transaction_response["application-index"]
for m in ABI.DISPATCH_TABLE.values():
m.asc_id = asc_idx
app_account = app_idx_to_account(asc_idx)
fund(master, app_account, amount=SC_MIN_BALANCE)
return app_account
def decode_state(state):
return {
# We are assuming that global space `key` are printable.
# If that's not necessarily true, we can change that.
base64.b64decode(s["key"]).decode(): base64.b64decode(s["value"]["bytes"])
if s["value"]["type"] == 1
else int(s["value"]["uint"])
for s in state
}
def get_application_state(asc_idx: int) -> dict[str, Union[bytes, int]]:
global_state = algod_client.application_info(asc_idx)["params"]["global-state"]
global_state = decode_state(global_state)
return global_state
def get_local_state(account: Account, asc_idx: int) -> dict[str, Union[bytes, int]]:
local_states = algod_client.account_info(account.address)["apps-local-state"]
local_state = [s for s in local_states if s["id"] == asc_idx][0].get(
"key-value", {}
)
local_state = decode_state(local_state)
return local_state
def get_account_balance(account: Account) -> dict[int, int]:
account_info = algod_client.account_info(account.address)
balances = {a["asset-id"]: int(a["amount"]) for a in account_info["assets"]}
balances[0] = int(account_info["amount"])
return balances
def get_account_asa_balance(account: Account, asa_idx: int) -> int:
return get_account_balance(account).get(asa_idx, 0)
def get_asset_info(asa_idx) -> dict:
return algod_client.asset_info(asa_idx)["params"]
def abi_call(
sender: Account,
method,
*args,
fee: Optional[int] = None,
) -> Any: # TODO: Correctly specify the return type here.
"""
ABI call from `sender` to `method`, with `*args`
Specify `n_app_calls` to add no-ops that will increase the AppCall budget.
`group_senders_txns` allows tailing other transactions to the ABI call in a
group; expects an iterable of pairs (sender, transaction).
"""
params = get_params(algod_client, fee)
encoded_args = []
for arg in args:
if isinstance(arg, Account):
encoded_args.append(arg.address)
else:
encoded_args.append(arg)
atc = AtomicTransactionComposer()
atc.add_method_call(
app_id=method.asc_id,
method=Method.from_signature(method.signature),
method_args=encoded_args,
sp=params,
sender=sender.address,
signer=sender,
)
atc.build_group()
atc_result = atc.execute(algod_client, MAX_WAIT_ROUNDS)
logged_result = atc_result.abi_results[0] # type: ignore
if not logged_result or not logged_result.return_value:
return atc_result.tx_ids[0]
return logged_result.return_value
def deploy():
faucet = find_sandbox_faucet()
print(f" --- ⛲ Sandbox faucet account: {faucet.address}.")
master = create_and_fund(faucet)
print(f" --- 💼 Created master account: {master.address}.")
print(f" --- 💼 Master account mnemonic:\n{master.mnemonic()}")
config = Config(master=AVMState.Address(master.address))
app_account = create_application(master, config)
asc_idx = app_account.app
assert asc_idx # `Account.app` is Optional, this puts the linter at rest.
print(
f" --- 🔧 Created and funded ASC: {asc_idx} with address {app_account.address}."
)
asa_idx = create_tc_asa(master, asc_idx)
print(f" --- 🥇 Created TC-ASA: {asa_idx}.")
abi_call(master, init, asa_idx)
print(" --- 🥇 Tied together the ASA into the ASC for the TC-ASA.")
user = create_and_fund(faucet)
print(f" --- 👨 Created user account: {user.address}.")
user = create_and_fund(faucet)
optin_to_application(user, asc_idx)
optin_to_asset(user, asa_idx)
print(f" --- User {user.address} opted into the TC-ASA (application and asset).")
contract_dict = ABI.to_contract_specification(
algod_client.versions()["genesis_hash_b64"],
asc_idx,
)
contract_path = "/tmp/contract.json"
print(f" --- Saving contract definition to '{contract_path}'.")
with open(contract_path, "w") as f:
json.dump(contract_dict, f, indent=2)
if __name__ == "__main__":
deploy()