-
Notifications
You must be signed in to change notification settings - Fork 16
/
deploy.py
502 lines (369 loc) · 16.9 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
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
import os
import base64
import time
from algosdk.v2client import algod, indexer
from algosdk.future import transaction
from algosdk import encoding, account, mnemonic, error
from pyteal import compileTeal, Mode
from contracts import manager
ALGOD_ENDPOINT = os.environ['ALGOD_ENDPOINT']
ALGOD_TOKEN = os.environ['ALGOD_TOKEN']
INDEXER_ENDPOINT = os.environ['INDEXER_ENDPOINT']
INDEXER_TOKEN = os.environ['INDEXER_TOKEN']
DEVELOPER_ACCOUNT_PRIVATE_KEY = mnemonic.to_private_key(
os.environ['DEVELOPER_ACCOUNT_PRIVATE_KEY'])
DEVELOPER_ACCOUNT_ADDRESS = account.address_from_private_key(
DEVELOPER_ACCOUNT_PRIVATE_KEY)
ZERO_ADDRESS = encoding.encode_address(bytes(32))
TEST_ACCOUNT_PRIVATE_KEY = mnemonic.to_private_key(
os.environ['TEST_ACCOUNT_PRIVATE_KEY'])
TEST_ACCOUNT_ADDRESS = account.address_from_private_key(
TEST_ACCOUNT_PRIVATE_KEY)
TOKEN1_UNIT_NAME = "TOKEN1"
TOKEN1_ASSET_NAME = "AlgoSwap Token 1 Test Asset"
TOKEN1_AMOUNT = 2**64 - 1
TOKEN1_DECIMALS = 6
TOKEN2_UNIT_NAME = "TOKEN2"
TOKEN2_ASSET_NAME = "AlgoSwap Token 2 Test Asset"
TOKEN2_AMOUNT = 2**64 - 1
TOKEN2_DECIMALS = 6
LIQUIDITY_TOKEN_UNIT_NAME = "T1T2"
LIQUIDITY_TOKEN_ASSET_NAME = "AlgoSwap Token1/Token2"
LIQUIDITY_TOKEN_AMOUNT = 2**64 - 1
LIQUIDITY_TOKEN_DECIMALS = 6
algod_client = algod.AlgodClient(ALGOD_TOKEN, ALGOD_ENDPOINT, headers={
"x-api-key": ALGOD_TOKEN})
indexer_client = indexer.IndexerClient(INDEXER_TOKEN, INDEXER_ENDPOINT, headers={
"x-api-key": INDEXER_TOKEN})
def wait_for_transaction(transaction_id):
suggested_params = algod_client.suggested_params()
algod_client.status_after_block(suggested_params.first + 4)
result = indexer_client.search_transactions(txid=transaction_id)
assert len(result['transactions']) == 1, result
return result['transactions'][0]
def compile_exchange_validator():
from contracts import validator
print("Compiling exchange validator application...")
validator_approve_teal_code = compileTeal(
validator.approval_program(), Mode.Application)
compile_response = algod_client.compile(validator_approve_teal_code)
validator_approve_code = base64.b64decode(compile_response['result'])
VALIDATOR_APPROVE_BYTECODE_LEN = len(validator_approve_code)
VALIDATOR_APPROVE_ADDRESS = compile_response['hash']
validator_clear_teal_code = compileTeal(
validator.clear_program(), Mode.Application)
compile_response = algod_client.compile(validator_clear_teal_code)
validator_clear_code = base64.b64decode(compile_response['result'])
VALIDATOR_CLEAR_BYTECODE_LEN = len(validator_clear_code)
VALIDATOR_CLEAR_ADDRESS = compile_response['hash']
print(
f"Exchange Validator | Approval: {VALIDATOR_APPROVE_BYTECODE_LEN}/1024 bytes ({VALIDATOR_APPROVE_ADDRESS}) | Clear: {VALIDATOR_CLEAR_BYTECODE_LEN}/1024 bytes ({VALIDATOR_CLEAR_ADDRESS})")
with open('./build/validator_approval.teal', 'w') as f:
f.write(validator_approve_teal_code)
with open('./build/validator_clear.teal', 'w') as f:
f.write(validator_clear_teal_code)
print()
return validator_approve_code, validator_clear_code
def compile_exchange_manager():
print("Compiling exchange manager application...")
manager_approve_teal_code = compileTeal(
manager.approval_program(), Mode.Application)
compile_response = algod_client.compile(manager_approve_teal_code)
manager_approve_code = base64.b64decode(compile_response['result'])
MANAGER_APPROVE_BYTECODE_LEN = len(manager_approve_code)
MANAGER_APPROVE_ADDRESS = compile_response['hash']
manager_clear_teal_code = compileTeal(
manager.clear_program(), Mode.Application)
compile_response = algod_client.compile(manager_clear_teal_code)
manager_clear_code = base64.b64decode(compile_response['result'])
MANAGER_CLEAR_BYTECODE_LEN = len(manager_clear_code)
MANAGER_CLEAR_ADDRESS = compile_response['hash']
print(
f"Exchange Manager | Approval: {MANAGER_APPROVE_BYTECODE_LEN}/1024 bytes ({MANAGER_APPROVE_ADDRESS}) | Clear: {MANAGER_CLEAR_BYTECODE_LEN}/1024 bytes ({MANAGER_CLEAR_ADDRESS})")
with open('./build/manager_approval.teal', 'w') as f:
f.write(manager_approve_teal_code)
with open('./build/manager_clear.teal', 'w') as f:
f.write(manager_clear_teal_code)
print()
return manager_approve_code, manager_clear_code
def compile_exchange_escrow():
from contracts import escrow
print("Compiling exchange escrow logicsig...")
escrow_logicsig_teal_code = compileTeal(
escrow.logicsig(), Mode.Application)
compile_response = algod_client.compile(escrow_logicsig_teal_code)
escrow_logicsig = compile_response['result']
escrow_logicsig_bytes = base64.b64decode(escrow_logicsig)
ESCROW_BYTECODE_LEN = len(escrow_logicsig_bytes)
ESCROW_ADDRESS = compile_response['hash']
print(
f"Exchange Escrow | {ESCROW_BYTECODE_LEN}/1000 bytes ({ESCROW_ADDRESS})")
with open('./build/escrow.teal', 'w') as f:
f.write(escrow_logicsig_teal_code)
with open("./build/escrow_logicsig", "w") as f:
f.write(escrow_logicsig)
print(f"Escrow logicsig compiled with address {ESCROW_ADDRESS}")
print()
return escrow_logicsig
def deploy_exchange_validator(validator_approve_code, validator_clear_code):
print("Deploying exchange validator application...")
create_validator_transaction = transaction.ApplicationCreateTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
on_complete=transaction.OnComplete.NoOpOC,
approval_program=validator_approve_code,
clear_program=validator_clear_code,
global_schema=transaction.StateSchema(num_uints=0, num_byte_slices=1),
local_schema=None,
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
tx_id = algod_client.send_transaction(create_validator_transaction)
validator_app_id = wait_for_transaction(tx_id)['created-application-index']
print(
f"Exchange Validator deployed with Application ID: {validator_app_id} (Txn ID: https://testnet.algoexplorer.io/tx/{tx_id})"
)
print()
return validator_app_id
def deploy_exchange_manager(manager_approve_code, manager_clear_code):
print("Deploying exchange manager application...")
create_manager_transaction = transaction.ApplicationCreateTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
on_complete=transaction.OnComplete.NoOpOC,
approval_program=manager_approve_code,
clear_program=manager_clear_code,
global_schema=transaction.StateSchema(num_uints=0, num_byte_slices=1),
local_schema=transaction.StateSchema(num_uints=10, num_byte_slices=0),
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
tx_id = algod_client.send_transaction(create_manager_transaction)
manager_app_id = wait_for_transaction(tx_id)['created-application-index']
print(
f"Exchange Manager deployed with Application ID: {manager_app_id} (Txn ID: https://testnet.algoexplorer.io/tx/{tx_id})"
)
print()
return manager_app_id
def deploy_token1_token2():
print(
f"Deploying tokens {TOKEN1_ASSET_NAME} ({TOKEN1_UNIT_NAME}) and {TOKEN2_ASSET_NAME} ({TOKEN2_UNIT_NAME})..."
)
txn_1 = transaction.AssetConfigTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
total=TOKEN1_AMOUNT,
default_frozen=False,
unit_name=TOKEN1_UNIT_NAME,
asset_name=TOKEN1_ASSET_NAME,
manager=DEVELOPER_ACCOUNT_ADDRESS,
reserve=DEVELOPER_ACCOUNT_ADDRESS,
freeze=DEVELOPER_ACCOUNT_ADDRESS,
clawback=DEVELOPER_ACCOUNT_ADDRESS,
url=f"https://algoswap.io/{TOKEN1_UNIT_NAME}",
decimals=TOKEN1_DECIMALS
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
txn_2 = transaction.AssetConfigTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
total=TOKEN2_AMOUNT,
default_frozen=False,
unit_name=TOKEN2_UNIT_NAME,
asset_name=TOKEN2_ASSET_NAME,
manager=DEVELOPER_ACCOUNT_ADDRESS,
reserve=DEVELOPER_ACCOUNT_ADDRESS,
freeze=DEVELOPER_ACCOUNT_ADDRESS,
clawback=DEVELOPER_ACCOUNT_ADDRESS,
url=f"https://algoswap.io/{TOKEN2_UNIT_NAME}",
decimals=TOKEN2_DECIMALS
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
tx_id_1 = algod_client.send_transaction(txn_1)
tx_id_2 = algod_client.send_transaction(txn_2)
token_1_asset_id = wait_for_transaction(tx_id_1)['created-asset-index']
token_2_asset_id = wait_for_transaction(tx_id_2)['created-asset-index']
print(
f"Deployed {TOKEN1_ASSET_NAME} ({TOKEN1_UNIT_NAME}) with Asset ID: {token_1_asset_id} | Tx ID: https://testnet.algoexplorer.io/tx/{tx_id_1}"
)
print(
f"Deployed {TOKEN2_ASSET_NAME} ({TOKEN2_UNIT_NAME}) with Asset ID: {token_2_asset_id} | Tx ID: https://testnet.algoexplorer.io/tx/{tx_id_2}"
)
print()
return token_1_asset_id, token_2_asset_id
def deploy_liquidity_pair_token():
print(
f"Deploying token {LIQUIDITY_TOKEN_ASSET_NAME} ({LIQUIDITY_TOKEN_UNIT_NAME})..."
)
txn = transaction.AssetConfigTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
total=LIQUIDITY_TOKEN_AMOUNT,
default_frozen=False,
unit_name=LIQUIDITY_TOKEN_UNIT_NAME,
asset_name=LIQUIDITY_TOKEN_ASSET_NAME,
manager=DEVELOPER_ACCOUNT_ADDRESS,
reserve=DEVELOPER_ACCOUNT_ADDRESS,
freeze=DEVELOPER_ACCOUNT_ADDRESS,
clawback=DEVELOPER_ACCOUNT_ADDRESS,
url=f"https://algoswap.io/{LIQUIDITY_TOKEN_UNIT_NAME}",
decimals=LIQUIDITY_TOKEN_DECIMALS
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
tx_id = algod_client.send_transaction(txn)
liquidity_token_asset_id = int(
wait_for_transaction(tx_id)['created-asset-index'])
print(
f"Deployed {LIQUIDITY_TOKEN_ASSET_NAME} ({LIQUIDITY_TOKEN_UNIT_NAME}) with Asset ID: {liquidity_token_asset_id} | Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
)
print()
return liquidity_token_asset_id
def opt_escrow_into_token(escrow_logicsig, token_idx):
print(
f"Opting Escrow into Token with Asset ID: {token_idx}..."
)
program = base64.b64decode(escrow_logicsig)
lsig = transaction.LogicSig(program)
txn = transaction.AssetTransferTxn(
sender=lsig.address(),
sp=algod_client.suggested_params(),
receiver=lsig.address(),
amt=0,
index=token_idx,
)
lsig_txn = transaction.LogicSigTransaction(txn, lsig)
tx_id = algod_client.send_transaction(lsig_txn)
wait_for_transaction(tx_id)
print(
f"Opted Escrow into Token with Asset ID: {token_idx} successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
)
print()
def opt_escrow_into_manager(escrow_logicsig, manager_app_id, liquidity_token_asset_id, token1_asset_id, token2_asset_id):
print("Opting Escrow into Manager contract...")
program = base64.b64decode(escrow_logicsig)
lsig = transaction.LogicSig(program)
args = [
liquidity_token_asset_id.to_bytes(8, 'big'),
token1_asset_id.to_bytes(8, 'big'),
token2_asset_id.to_bytes(8, 'big')
]
txn = transaction.ApplicationOptInTxn(
sender=lsig.address(),
sp=algod_client.suggested_params(),
index=manager_app_id,
app_args=args
)
lsig_txn = transaction.LogicSigTransaction(txn, lsig)
tx_id = algod_client.send_transaction(lsig_txn)
wait_for_transaction(tx_id)
print(
f"Opted Escrow into Manager contract successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
)
print()
def opt_user_into_contract(app_id):
print(
f"Opting user into contract with App ID: {app_id}..."
)
txn = transaction.ApplicationOptInTxn(
sender=TEST_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
index=app_id
).sign(TEST_ACCOUNT_PRIVATE_KEY)
tx_id = algod_client.send_transaction(txn)
wait_for_transaction(tx_id)
print(
f"Opted user into contract with App ID: {app_id} successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
)
print()
def opt_user_into_token(asset_id):
print(
f"Opting user into token with Asset ID: {asset_id}..."
)
txn = transaction.AssetTransferTxn(
sender=TEST_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
receiver=TEST_ACCOUNT_ADDRESS,
amt=0,
index=asset_id
).sign(TEST_ACCOUNT_PRIVATE_KEY)
tx_id = algod_client.send_transaction(txn)
wait_for_transaction(tx_id)
print(
f"Opted user into token with Asset ID: {asset_id} successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
)
print()
def transfer_liquidity_token_to_escrow(liquidity_token_asset_id, escrow_logicsig):
print(
f"Transferring {LIQUIDITY_TOKEN_AMOUNT} liquidity token with Asset ID: {liquidity_token_asset_id} to Escrow..."
)
program = base64.b64decode(escrow_logicsig)
lsig = transaction.LogicSig(program)
txn = transaction.AssetTransferTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
receiver=lsig.address(),
amt=LIQUIDITY_TOKEN_AMOUNT,
index=liquidity_token_asset_id
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
tx_id = algod_client.send_transaction(txn)
wait_for_transaction(tx_id)
print(
f"Transferred {LIQUIDITY_TOKEN_AMOUNT} liquidity token with Asset ID: {liquidity_token_asset_id} to Escrow successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}"
)
print()
def transfer_token1_token2_to_user(token1_asset_id, token2_asset_id):
print(
f"Transferring {int(TOKEN1_AMOUNT/2)} {TOKEN1_ASSET_NAME} ({TOKEN1_UNIT_NAME}) with Asset ID: {token1_asset_id} and {int(TOKEN2_AMOUNT/2)} {TOKEN2_ASSET_NAME} ({TOKEN2_UNIT_NAME}) with Asset ID: {token2_asset_id} to User..."
)
txn_1 = transaction.AssetTransferTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
receiver=TEST_ACCOUNT_ADDRESS,
amt=int(TOKEN1_AMOUNT/2),
index=token1_asset_id
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
txn_2 = transaction.AssetTransferTxn(
sender=DEVELOPER_ACCOUNT_ADDRESS,
sp=algod_client.suggested_params(),
receiver=TEST_ACCOUNT_ADDRESS,
amt=int(TOKEN2_AMOUNT/2),
index=token2_asset_id
).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY)
tx_id_1 = algod_client.send_transaction(txn_1)
tx_id_2 = algod_client.send_transaction(txn_2)
wait_for_transaction(tx_id_1)
wait_for_transaction(tx_id_2)
print(
f"Transferred {int(TOKEN1_AMOUNT/2)} {TOKEN1_ASSET_NAME} ({TOKEN1_UNIT_NAME}) with Asset ID: {token1_asset_id} to User successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id_1}"
)
print(
f"Transferred {int(TOKEN2_AMOUNT/2)} {TOKEN2_ASSET_NAME} ({TOKEN2_UNIT_NAME}) with Asset ID: {token2_asset_id} to User successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id_2}"
)
print()
if __name__ == "__main__":
print("Starting deployment process...")
manager_approve_code, manager_clear_code = compile_exchange_manager()
manager_app_id = deploy_exchange_manager(
manager_approve_code, manager_clear_code)
input(f"Update Manager Index in Validator = {manager_app_id}")
validator_approve_code, validator_clear_code = compile_exchange_validator()
validator_app_id = deploy_exchange_validator(
validator_approve_code, validator_clear_code)
token1_asset_id, token2_asset_id = deploy_token1_token2()
liquidity_token_asset_id = deploy_liquidity_pair_token()
params = algod_client.suggested_params()
print("Please update the Escrow contract with the following:")
input(f"Manager App ID = {manager_app_id}")
input(f"Validator App ID = {validator_app_id}")
input(f"Token 1 Asset ID = {token1_asset_id}")
input(f"Token 2 Asset ID = {token2_asset_id}")
input(f"Liquidity Token Asset ID = {liquidity_token_asset_id}")
input(f"Last Valid Round = {params.last + 100}")
escrow_logicsig = compile_exchange_escrow()
input("Please fund the Escrow account with $ALGO to continue")
opt_escrow_into_token(escrow_logicsig, token1_asset_id)
opt_escrow_into_token(escrow_logicsig, token2_asset_id)
opt_escrow_into_token(escrow_logicsig, liquidity_token_asset_id)
opt_escrow_into_manager(escrow_logicsig, manager_app_id,
liquidity_token_asset_id, token1_asset_id, token2_asset_id)
opt_user_into_contract(validator_app_id)
opt_user_into_contract(manager_app_id)
opt_user_into_token(token1_asset_id)
opt_user_into_token(token2_asset_id)
opt_user_into_token(liquidity_token_asset_id)
transfer_liquidity_token_to_escrow(liquidity_token_asset_id, escrow_logicsig)
transfer_token1_token2_to_user(token1_asset_id, token2_asset_id)
print("Deployment completed successfully!")