def app_create_txn(): approve_app = b'\x02 \x05\x00\x05\x04\x02\x01&\x07\x04vote\tVoteBegin\x07VoteEnd\x05voted\x08RegBegin\x06RegEnd\x07Creator1\x18"\x12@\x00\x951\x19\x12@\x00\x871\x19$\x12@\x00y1\x19%\x12@\x00R1\x19!\x04\x12@\x00<6\x1a\x00(\x12@\x00\x01\x002\x06)d\x0f2\x06*d\x0e\x10@\x00\x01\x00"2\x08+c5\x005\x014\x00A\x00\x02"C6\x1a\x016\x1a\x01d!\x04\x08g"+6\x1a\x01f!\x04C2\x06\'\x04d\x0f2\x06\'\x05d\x0e\x10C"2\x08+c5\x005\x012\x06*d\x0e4\x00\x10A\x00\t4\x014\x01d!\x04\tg!\x04C1\x00\'\x06d\x12C1\x00\'\x06d\x12C\'\x061\x00g1\x1b$\x12@\x00\x01\x00\'\x046\x1a\x00\x17g\'\x056\x1a\x01\x17g)6\x1a\x02\x17g*6\x1a\x03\x17g!\x04C' clear_pgm = b'\x02 \x01\x01' # the approve_app is a compiled Tealscript taken from https://pyteal.readthedocs.io/en/stable/examples.html#periodic-payment # we truncated the pgm because of the memory limit on the Ledeger approve_app = approve_app[:128] local_ints = 2 local_bytes = 5 global_ints = 24 global_bytes = 1 global_schema = transaction.StateSchema(global_ints, global_bytes) local_schema = transaction.StateSchema(local_ints, local_bytes) local_sp = transaction.SuggestedParams(fee= 2100, first=6002000, last=6003000, gen="testnet-v1.0", gh="SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",flat_fee=True) txn = algosdk.future.transaction.ApplicationCreateTxn(sender="YK54TGVZ37C7P76GKLXTY2LAH2522VD3U2434HRKE7NMXA65VHJVLFVOE4", sp=local_sp, approval_program=approve_app, on_complete=transaction.OnComplete.NoOpOC.real,clear_program= clear_pgm, global_schema=global_schema, foreign_apps=[55], foreign_assets=[31566704], accounts=["7PKXMJB2577SQ6R6IGYRAZQ27TOOOTIGTOQGJB3L5SGZFBVVI4AHMKLCEI", "NWBZBIROXZQEETCDKX6IZVVBV4EY637KCIX56LE5EHIQERCTSDYGXWG6PU"], app_args=[b'\x00\x00\0x00\x00',b'\x02\x00\0x00\x00'], local_schema=local_schema ) return txn
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 create_app( client, creator, creator_priv_key, suggested_params, asset_index, ): txn = transaction.ApplicationCreateTxn( creator, suggested_params, transaction.OnComplete.NoOpOC.real, compile_program(client, open('./contracts/state.teal', 'rb').read()), compile_program(client, open('./contracts/clear.teal', 'rb').read()), transaction.StateSchema(num_byte_slices=2, num_uints=4), transaction.StateSchema(num_byte_slices=0, num_uints=3), app_args=[int_to_bytes(asset_index)]) signed_txn = txn.sign(creator_priv_key) tx_id = client.send_transactions([signed_txn]) return tx_id
def main(): # initialize an algodClient algod_client = algod.AlgodClient(algod_token, algod_address) # define private keys creator_private_key = get_private_key_from_mnemonic(creator_mnemonic) user_private_key = get_private_key_from_mnemonic(user_mnemonic) # declare application state storage (immutable) local_ints = 0 local_bytes = 1 global_ints = 24 # 4 for setup + 20 for choices. Use a larger number for more choices. global_bytes = 1 global_schema = transaction.StateSchema(global_ints, global_bytes) local_schema = transaction.StateSchema(local_ints, local_bytes) # get PyTeal approval program approval_program_ast = approval_program() # compile program to TEAL assembly approval_program_teal = compileTeal(approval_program_ast, Mode.Application) # compile program to binary approval_program_compiled = compile_program(algod_client, approval_program_teal) # get PyTeal clear state program clear_state_program_ast = clear_state_program() # compile program to TEAL assembly clear_state_program_teal = compileTeal(clear_state_program_ast, Mode.Application) # compile program to binary clear_state_program_compiled = compile_program(algod_client, clear_state_program_teal) # configure registration and voting period status = algod_client.status() regBegin = status['last-round'] + 10 regEnd = regBegin + 10 voteBegin = regEnd + 1 voteEnd = voteBegin + 10 print(f"Registration rounds: {regBegin} to {regEnd}") print(f"Vote rounds: {voteBegin} to {voteEnd}") # create list of bytes for app args app_args = [ intToBytes(regBegin), intToBytes(regEnd), intToBytes(voteBegin), intToBytes(voteEnd) ] # create new application app_id = create_app(algod_client, creator_private_key, approval_program_compiled, clear_state_program_compiled, global_schema, local_schema, app_args) # read global state of application print( "Global state:", read_global_state( algod_client, account.address_from_private_key(creator_private_key), app_id)) # wait for registration period to start wait_for_round(algod_client, regBegin) # opt-in to application opt_in_app(algod_client, user_private_key, app_id) wait_for_round(algod_client, voteBegin) # call application without arguments call_app(algod_client, user_private_key, app_id, [b'vote', b'choiceA']) # read local state of application from user account print( "Local state:", read_local_state(algod_client, account.address_from_private_key(user_private_key), app_id)) # wait for registration period to start wait_for_round(algod_client, voteEnd) # read global state of application global_state = read_global_state( algod_client, account.address_from_private_key(creator_private_key), app_id) print("Global state:", global_state) max_votes = 0 max_votes_choice = None for key, value in global_state.items(): if key not in ('RegBegin', 'RegEnd', 'VoteBegin', 'VoteEnd', 'Creator') and isinstance(value, int): if value > max_votes: max_votes = value max_votes_choice = key print("The winner is:", max_votes_choice) # delete application delete_app(algod_client, creator_private_key, app_id) # clear application from user account clear_app(algod_client, user_private_key, app_id)
def coerce_schema(self, values): if not values: return None if isinstance(values, txn.StateSchema): return values return txn.StateSchema(num_uints=values[0], num_byte_slices=values[1])
from algosdk.v2client import algod # user declared account mnemonics creator_mnemonic = "Your 25-word mnemonic goes here" user_mnemonic = "A second distinct 25-word mnemonic goes here" # user declared algod connection parameters algod_address = "http://localhost:4001" algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" # declare application state storage (immutable) local_ints = 1 local_bytes = 1 global_ints = 1 global_bytes = 0 global_schema = transaction.StateSchema(global_ints, global_bytes) local_schema = transaction.StateSchema(local_ints, local_bytes) # user declared approval program (initial) approval_program_source_initial = b"""#pragma version 2 // Handle each possible OnCompletion type. We don't have to worry about // handling ClearState, because the ClearStateProgram will execute in that // case, not the ApprovalProgram. txn OnCompletion int NoOp == bnz handle_noop txn OnCompletion int OptIn
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']) print( f"Exchange manager clear program currently uses {len(manager_clear_code)} of 1000 bytes" ) print("Deploying exchange manager application...") create_application_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=7, num_byte_slices=1), ).sign(DEVELOPER_ACCOUNT_PRIVATE_KEY) transaction_id = algod_client.send_transaction( create_application_transaction) exchange_application_id = wait_for_transaction( transaction_id)['created-application-index'] print( f"Exchange manager deployed with application ID {exchange_application_id} (transaction: {transaction_id})" ) # deploy escrow escrow_teal_code = compileTeal(escrow.logicsig(), Mode.Application) compile_response = algod_client.compile(escrow_teal_code) ESCROW_ADDRESS = compile_response['hash'] print("ESCROW_ADDRESS:", ESCROW_ADDRESS)