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 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_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 _build_logicsig_txn(program, txn, lsig): """ Helper function to make LogicSigTransaction """ # replacing program with an empty one is OK since it set by source # LogicSig does not like None/invalid programs because of validation program = program if isinstance(program, bytes) else b'\x01' logicsig = transaction.LogicSig(program, lsig.args) return transaction.LogicSigTransaction(txn, logicsig)
def escrow_opt_in_to_asset( client, suggested_params, address, asset_index, ): lsig = transaction.LogicSig( compile_program(client, open('./contracts/escrow.teal', 'rb').read())) asset_opt_in = transaction.AssetTransferTxn(address, suggested_params, address, 0, asset_index) signed_asset_opt_in = transaction.LogicSigTransaction(asset_opt_in, lsig) tx_id = client.send_transactions([signed_asset_opt_in]) return tx_id
def stateless_to_lsig(note): escrow = escrow_account(note) teal_escrow = compile_stateless(escrow) # Write the teal program on disk for inspectability � with open('escrow.teal', 'w') as f: f.write(teal_escrow) compiled = algod_client.compile(teal_escrow) program = base64.decodebytes(compiled["result"].encode()) lsig = transaction.LogicSig(program) return Account(address=lsig.address(), private_key=None), lsig
def withdraw_call( client, user, user_priv_key, suggested_params, app_id, escrow_addr, asset_index, algos_amount=0, asset_amount=0, ): app_txn = transaction.ApplicationCallTxn( user, suggested_params, app_id, transaction.OnComplete.NoOpOC.real, app_args=['WITHDRAW'.encode('utf-8')]) asset_withdraw_txn = transaction.AssetTransferTxn( escrow_addr, suggested_params, user, asset_amount, asset_index, ) algos_withdraw_txn = transaction.PaymentTxn( escrow_addr, suggested_params, user, algos_amount, ) gid = transaction.calculate_group_id( [app_txn, asset_withdraw_txn, algos_withdraw_txn]) app_txn.group = gid asset_withdraw_txn.group = gid algos_withdraw_txn.group = gid lsig = transaction.LogicSig( compile_program(client, open('./contracts/escrow.teal', 'rb').read())) signed_asset_withdraw_txn = transaction.LogicSigTransaction( asset_withdraw_txn, lsig) signed_algos_withdraw_txn = transaction.LogicSigTransaction( algos_withdraw_txn, lsig) signed_app_txn = app_txn.sign(user_priv_key) tx_id = client.send_transactions( [signed_app_txn, signed_asset_withdraw_txn, signed_algos_withdraw_txn]) return tx_id
# now, as a provider, you can withdraw Algo from the escrow if you sign the first valid acl = algod.AlgodClient(params.algod_token, params.algod_address) sp = acl.suggested_params_as_object() first_valid = sp.first data = first_valid.to_bytes(8, byteorder='big') lease = hash.sha256(data) lease_bytes = encoding.HexEncoder.decode(lease) print("first valid: {}".format(first_valid)) txn = transaction.PaymentTxn(escrow_addr, sp, provider_addr, 100000, lease=lease_bytes) with open(lsig_fname, "rb") as f: teal_bytes = f.read() lsig = transaction.LogicSig(teal_bytes) lstx = transaction.LogicSigTransaction(txn, lsig) assert(lstx.verify()) # send LogicSigTransaction to network transaction.write_to_file([lstx], "r_s_1.txn") stdout, stderr = execute(["goal", "clerk", "tealsign", "--data-b64", base64.b64encode(data), "--lsig-txn", "r_s_1.txn", "--keyfile", key_fn, "--set-lsig-arg-idx", "0"]) if stderr != "": print(stderr) raise print(stdout)
def withdraw_protocol_fees(): print("Building withdraw protocol fees atomic transaction group...") encoded_app_args = [bytes("p", "utf-8")] # Get unclaimed TOKEN1 and TOKEN2 protocol fees protocol_unused_token1 = protocol_unused_token2 = 0 account_info = algod_client.account_info(ESCROW_ADDRESS) local_state = account_info['apps-local-state'] for block in local_state: if block['id'] == MANAGER_INDEX: for kvs in block['key-value']: decoded_key = base64.b64decode(kvs['key']) prefix_bytes = decoded_key[:2] prefix_key = prefix_bytes.decode('utf-8') if (prefix_key == "P1"): protocol_unused_token1 = kvs['value']['uint'] elif (prefix_key == "P2"): protocol_unused_token2 = kvs['value']['uint'] print( f"Unused Protocol Fees is Token 1 = {protocol_unused_token1} and Token 2 = {protocol_unused_token2}" ) if protocol_unused_token1 != 0 and protocol_unused_token2 != 0: # Transaction to Validator txn_1 = transaction.ApplicationCallTxn( sender=DEVELOPER_ACCOUNT_ADDRESS, sp=algod_client.suggested_params(), index=VALIDATOR_INDEX, on_complete=transaction.OnComplete.NoOpOC, accounts=[ESCROW_ADDRESS], app_args=encoded_app_args) # Transaction to Manager txn_2 = transaction.ApplicationCallTxn( sender=DEVELOPER_ACCOUNT_ADDRESS, sp=algod_client.suggested_params(), index=MANAGER_INDEX, on_complete=transaction.OnComplete.NoOpOC, accounts=[ESCROW_ADDRESS], app_args=encoded_app_args) # Transaction to send Token 1 from Escrow to Developer txn_3 = transaction.AssetTransferTxn( sender=ESCROW_ADDRESS, sp=algod_client.suggested_params(), receiver=DEVELOPER_ACCOUNT_ADDRESS, amt=protocol_unused_token1, index=TOKEN1_INDEX) # Transaction to send Token 2 from Escrow to Developer txn_4 = transaction.AssetTransferTxn( sender=ESCROW_ADDRESS, sp=algod_client.suggested_params(), receiver=DEVELOPER_ACCOUNT_ADDRESS, amt=protocol_unused_token2, index=TOKEN2_INDEX) # Make Logicsig program = base64.b64decode(ESCROW_LOGICSIG) lsig = transaction.LogicSig(program) # Get group ID and assign to transactions gid = transaction.calculate_group_id([txn_1, txn_2, txn_3, txn_4]) txn_1.group = gid txn_2.group = gid txn_3.group = gid txn_4.group = gid # Sign transactions stxn_1 = txn_1.sign(DEVELOPER_ACCOUNT_PRIVATE_KEY) stxn_2 = txn_2.sign(DEVELOPER_ACCOUNT_PRIVATE_KEY) stxn_3 = transaction.LogicSigTransaction(txn_3, lsig) stxn_4 = transaction.LogicSigTransaction(txn_4, lsig) # Broadcast the transactions signed_txns = [stxn_1, stxn_2, stxn_3, stxn_4] tx_id = algod_client.send_transactions(signed_txns) # Wait for transaction wait_for_transaction(tx_id) print( f"Withdrew protocol fees for Token 1 and Token 2 from AlgoSwap to Developer successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}" ) print()
# Example: creating a LogicSig transaction signed by a program that never approves the transfer. import tokens from algosdk import algod, account from algosdk.future import transaction program = b"\x01\x20\x01\x00\x22" # int 0 lsig = transaction.LogicSig(program) sender = lsig.address() receiver = account.generate_account() # create an algod client acl = algod.AlgodClient(tokens.algod_token, tokens.algod_address) # get suggested parameters sp = acl.suggested_params() # create a transaction amount = 10000 txn = transaction.PaymentTxn(sender, sp, receiver, amount) # note: transaction is signed by logic only (no delegation) # that means sender address must match to program hash lstx = transaction.LogicSigTransaction(txn, lsig) assert lstx.verify() # send them over network acl.send_transaction(lstx)
def sign_with_program(self, tx, program, delegator=None): if delegator: raise Exception("haven't implemented delgated logicsig yet") return txn.LogicSigTransaction(tx, txn.LogicSig(program))
def get_token1_refund(): print("Attempting to get refund of Token 1 from Escrow...") encoded_app_args = [bytes("r", "utf-8")] # Calculate unused_token1 unused_token1 = 0 account_info = algod_client.account_info(TEST_ACCOUNT_ADDRESS) local_state = account_info['apps-local-state'] for block in local_state: if block['id'] == MANAGER_INDEX: for kvs in block['key-value']: decoded_key = base64.b64decode(kvs['key']) prefix_bytes = decoded_key[:2] prefix_key = prefix_bytes.decode('utf-8') addr_bytes = decoded_key[2:] b32_encoded_addr = base64.b32encode(addr_bytes).decode('utf-8') escrow_addr = encoding.encode_address( base64.b32decode(b32_encoded_addr)) if (prefix_key == "U1" and ESCROW_ADDRESS == escrow_addr): unused_token1 = int(kvs['value']['uint']) print(f"User unused Token 1 is {unused_token1}") if unused_token1 != 0: # Transaction to Validator txn_1 = transaction.ApplicationCallTxn( sender=TEST_ACCOUNT_ADDRESS, sp=algod_client.suggested_params(), index=VALIDATOR_INDEX, on_complete=transaction.OnComplete.NoOpOC, accounts=[ESCROW_ADDRESS], app_args=encoded_app_args) # Transaction to Manager txn_2 = transaction.ApplicationCallTxn( sender=TEST_ACCOUNT_ADDRESS, sp=algod_client.suggested_params(), index=MANAGER_INDEX, on_complete=transaction.OnComplete.NoOpOC, accounts=[ESCROW_ADDRESS], app_args=encoded_app_args) # Make LogicSig program = base64.b64decode(ESCROW_LOGICSIG) lsig = transaction.LogicSig(program) # Transaction to get refund of Token 1 from Escrow txn_3 = transaction.AssetTransferTxn( sender=ESCROW_ADDRESS, sp=algod_client.suggested_params(), receiver=TEST_ACCOUNT_ADDRESS, amt=unused_token1, index=TOKEN1_INDEX) # Get group ID and assign to transactions gid = transaction.calculate_group_id([txn_1, txn_2, txn_3]) txn_1.group = gid txn_2.group = gid txn_3.group = gid # Sign transactions stxn_1 = txn_1.sign(TEST_ACCOUNT_PRIVATE_KEY) stxn_2 = txn_2.sign(TEST_ACCOUNT_PRIVATE_KEY) stxn_3 = transaction.LogicSigTransaction(txn_3, lsig) # Broadcast the transactions signed_txns = [stxn_1, stxn_2, stxn_3] tx_id = algod_client.send_transactions(signed_txns) print( f"Got refund of Token 1 from AlgoSwap to User successfully! Tx ID: https://testnet.algoexplorer.io/tx/{tx_id}" ) print()
INDEXER_ADDRESS = "https://mainnet-algorand.api.purestake.io/idx2" ALGOD_ADDRESS = "https://mainnet-algorand.api.purestake.io/ps2" REWARDS_POOL = '737777777777777777777777777777777777777777777777777UFEJ2CI' ALGOREALM_FIRST_BLOCK = 13578170 ALGOREALM_APP_ID = 137491307 CROWN_ID = 137493252 SCEPTRE_ID = 137494385 ALGOREALM_LAW_BYTECODE = \ 'AiAIAwbr5sdBAQSE9sdB8f7HQegHJgEg/v////////////////////////////////////' \ '////8yBCISMwAQIxIzABgkEhAQMwEQJRIzAQAzAAASEDMBBygSEBAzAhAhBBIzAhQzAQAS' \ 'EDMCESEFEjMCESEGEhEQMwISJRIQMwIBIQcOEDMCFTIDEhAzAiAyAxIQEA==' ALGOREALM_LAW_LSIG = transaction.LogicSig( base64.decodebytes(ALGOREALM_LAW_BYTECODE.encode())) ALGOREALM_LAW = Account( address=ALGOREALM_LAW_LSIG.address(), private_key=None, lsig=ALGOREALM_LAW_LSIG, ) ALGOREALM_CARD_FIRST_BLOCK = 16250000 ROYALTY_PERC = 5 ROYALTY_COLLECTOR_1 = Account( address='H7N65NZIWBOKFDSRNPLLDGN72HVFKXT4RRSY7M66B6Y2PFLQFKLPLHU5JU', private_key='') ROYALTY_COLLECTOR_2 = Account( address='2PDM3E7WLVPMEKCCMNTHM3FCZNZM4CSJQUOC4SWHMFPAR3N4NXBLCQKHPE', private_key='')