def transfer(passphrase=None) -> None: """ Creates an unsigned transfer transaction that is then sent off for signing and submission to the blockchain via the `sign_and_send` function. The transaction is submitted for the specified asset_id, to the specified address, for the specified amount. :param passphrase -> ``str``: the senders passphrase (used to sign the transaction) :return -> ``None``: """ amount = 100 transfer_data = { "sender": creator_address, "receiver": receiver_address, "amt": amount, "index": asset_id, "flat_fee": True } data = add_network_params(client, transfer_data) transaction = AssetTransferTxn(**data) if passphrase: transaction_info = sign_and_send(transaction, passphrase, client) formatted_amount = balance_formatter(amount, asset_id, client) print("Transferred {} from {} to {}".format(formatted_amount, creator_address, receiver_address)) print("Transaction ID Confirmation: {}".format( transaction_info.get("tx"))) else: write_to_file([transaction], "transfer.txn")
def test_file_read_write(self): # get suggested parameters and fee params = self.acl.suggested_params() gh = params["genesishashb64"] last_round = params["lastRound"] fee = params["fee"] # create transaction txn = transaction.PaymentTxn(self.account_0, fee, last_round, last_round + 100, gh, self.account_0, 1000) # get private key w = wallet.Wallet(wallet_name, wallet_pswd, self.kcl) private_key = w.export_key(self.account_0) # sign transaction stx = txn.sign(private_key) # write to file dir_path = os.path.dirname(os.path.realpath(__file__)) transaction.write_to_file([txn, stx], dir_path + "/raw.tx") # read from file txns = transaction.retrieve_from_file(dir_path + "/raw.tx") # check that the transactions are still the same self.assertEqual(encoding.msgpack_encode(txn), encoding.msgpack_encode(txns[0])) self.assertEqual(encoding.msgpack_encode(stx), encoding.msgpack_encode(txns[1])) # delete the file os.remove("raw.tx")
def create(passphrase=None): """ Returns an unsigned txn object and writes the unsigned transaction object to a file for offline signing. Uses current network params. """ print(headers) data = add_network_params(asset_details, client) txn = AssetConfigTxn(**data) if passphrase: txinfo = sign_and_send(txn, passphrase, client) print("Create asset confirmation, txid: {}".format(txinfo.get('tx'))) asset_id = txinfo['txresults'].get('createdasset') print("Asset ID: {}".format(asset_id)) else: write_to_file([txn], "create_coin.txn")
def create(passphrase: str = None) -> None: """ creates the asset that is defined in `config.py`, signs it, and sends it to the algorand network if the senders passphrase is supplied. Otherwise, the transaction is written to a file. :param passphrase -> ``str``: the user passphrase. :return -> `None`: """ transaction_data = add_network_params(client, asset_details) transaction = AssetConfigTxn(**transaction_data) if passphrase: transaction_info = sign_and_send(transaction, passphrase, client) print(f"Create asset confirmation, transaction ID: {transaction}") asset_id = transaction_info['txresults'].get('createdasset') print(f"Asset ID: {asset_id}") else: write_to_file([transaction], "create_coin.txn")
def optin(passphrase=None): """ Creates an unsigned opt-in transaction for the specified asset id and address. Uses current network params. """ optin_data = { "sender": receiver_address, "receiver": receiver_address, "amt": 0, "index": asset_id } data = add_network_params(optin_data, client) txn = AssetTransferTxn(**data) if passphrase: txinfo = sign_and_send(txn, passphrase, client) print("Opted in to asset ID: {}".format(asset_id)) print("Transaction ID Confirmation: {}".format(txinfo.get("tx"))) else: write_to_file([txn], "optin.txn")
def send_txns(self, txns: PaymentTxn or AssetTransferTxn): """ send txn to the algorand network :param txns -> ``PaymentTxn`` or ``AssetTransferTxn``: list of ``PaymentTxn`` or ``AssetTransferTxn`` transactions to. """ def handle_algo_txn(): transaction_info = sign_and_send(txn, self.algo_exchange_passphrase, self.client) formatted_amount = balance_formatter(self.amount, asset_id, self.client) print("Transferred {} Algos from {} to {}".format( self.amount, self.receiver_address, self.sender)) print("Transaction ID Confirmation: {}".format( transaction_info.get("tx"))) def handle_etf_txn(): transaction_info = sign_and_send(txn, self.buy_or_sell_passphrase, self.client) formatted_amount = balance_formatter(self.amount, asset_id, self.client) print("Transferred {} from {} to {}".format( formatted_amount, self.sender, self.receiver_address)) print("Transaction ID Confirmation: {}".format( transaction_info.get("tx"))) for txn in txns: if isinstance(txn, PaymentTxn): if self.algo_exchange_passphrase: handle_algo_txn() else: write_to_file([txn], "transfer.txn") elif isinstance(txn, AssetTransferTxn): if self.buy_or_sell_passphrase: handle_etf_txn() else: write_to_file([txn], "transfer.txn")
def transfer(passphrase=None): """ Creates an unsigned transfer transaction for the specified asset id, to the specified address, for the specified amount. """ amount = 6000 transfer_data = { "sender": creator_address, "receiver": receiver_address, "amt": amount, "index": asset_id } data = add_network_params(transfer_data, client) txn = AssetTransferTxn(**data) if passphrase: txinfo = sign_and_send(txn, passphrase, client) formatted_amount = balance_formatter(amount, asset_id, client) print("Transferred {} from {} to {}".format(formatted_amount, creator_address, receiver_address)) print("Transaction ID Confirmation: {}".format(txinfo.get("tx"))) else: write_to_file([txn], "transfer.txn")
def opt_in(passphrase: str = None) -> None: """ Creates, signs, and sends an opt-in transaction for the specified asset_id. If the passphrase is not supplied, writes the unsigned transaction to a file. :param passphrase -> ``str``: the user passphrase. :return -> `None`: """ opt_in_data = { "sender": creator_address, "receiver": receiver_address, "amt": 10, "index": asset_id } transaction_data = add_network_params(client, opt_in_data) transaction = AssetTransferTxn(**transaction_data) if passphrase: txinfo = sign_and_send(transaction, passphrase, client) created_asset_id = txinfo['txresults'].get('createdasset') print("Opted in to asset ID: {}".format(created_asset_id)) print("Transaction ID Confirmation: {}".format(txinfo.get("tx"))) else: write_to_file([transaction], "optin.txn")
def main(): txn1 = scenario1() txn2 = scenario2() txn3 = scenario3() txn4 = scenario4() txn5a, txn5b = scenario5() print("Writing transactions to files...") transaction.write_to_file([txn1, txn2, txn3, txn4], 'alice_sender_testnet.tx') transaction.write_to_file([txn5a], "alice_bob_no_multisig.tx") transaction.write_to_file([txn5b], "alice_bob_with_multisig.tx") print("Done.")
receiver = "ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM" lease = base64.b64decode("y9OJ5MRLCHQj8GqbikAUKMBI7hom+SOj8dlopNdNHXI=") # create a transaction txn = transaction.PaymentTxn(addr, fee, startRound, endRound, gh, receiver, amount, flat_fee=True, lease=lease) # Create the LogicSigTransaction with contract account LogicSig lstx = transaction.LogicSigTransaction(txn, lsig) # write to file txns = [lstx] transaction.write_to_file(txns, "p_pay.stxn") # send raw LogicSigTransaction to network txid = acl.send_transaction(lstx) print("Transaction ID: " + txid) # except Exception as e: # print(e) # send raw LogicSigTransaction again to network txid = acl.send_transaction(lstx) print("Transaction ID: " + txid)
def write_txn(context): dir_path = os.path.dirname(os.path.realpath(__file__)) dir_path = os.path.dirname(os.path.dirname(dir_path)) transaction.write_to_file([context.txn], dir_path + "/temp/raw" + context.num + ".tx")
def init_atomic(update, context): """ Utility for setting up an atomic transaction. Must be Initiated by the seller of an ASA. Can only trade an Algorand Asset for ALGO (Algorand's native currency) :return: Boolean - True """ asset = {'DMT2': 13251912} default = os.getenv('DEFAULT') buyer = context.user_data['buyer_address'] amount = context.user_data['amount_in_algo'] amount *= 1000000 asset_name = context.user_data['asset_name'] asset_name = asset_name.upper() sk = context.user_data['signing_key'] qty = context.user_data['quantity_in_ASA'] global ex_file if asset_name in asset: ex_file = ex_file + (buyer,) sell_addr = account.address_from_private_key(sk) update.message.reply_text('Setting up trade between: \n' 'Seller: 'f'{sell_addr}' ' and \n' 'buyer' f'{buyer}') prms = client.suggested_params() fst = prms.first lst = fst + 1000 gn = prms.gen gh = prms.gh fee = prms.fee flat_fee = 1000000 _fee_qty = 1 if amount in range(50, 10001): flat_fee = flat_fee amount = round(amount - flat_fee) qty -= _fee_qty elif amount > 10000: flat_fee = 2 amount = round(amount - flat_fee) _fee_qty = 3 qty -= _fee_qty else: flat_fee = flat_fee _fee_qty = _fee_qty string_b = sk.encode('utf-8') d = '%*'.encode('utf-8') b_bytes = base64.b64encode(string_b, d) write_to_file(update, context, buyer, b_bytes) # Payment transaction bd_unsigned = transaction.PaymentTxn(buyer, fee, fst, lst, gh, sell_addr, amount, None, None, gn, False, None) # Payment transaction (fee) bf_unsigned = transaction.PaymentTxn(buyer, fee, fst, lst, gh, default, flat_fee, None, None, gn, False, None) # Asset transfer txn sd_unsigned = AssetTransferTxn(sell_addr, prms, buyer, qty, asset[asset_name], None, None, None, None) # Asset transfer (fee) sf_unsigned = AssetTransferTxn(sell_addr, prms, default, _fee_qty, asset[asset_name], None, None, None, None) stg_path = os.path.dirname(os.path.realpath(__file__)) file = buyer[0:11] file_name = "./asa{}.txn".format(file) wtf = transaction.write_to_file([bd_unsigned, bf_unsigned, sd_unsigned, sf_unsigned], stg_path + file_name) key = buyer[:10] TRANSACTIONS[f'{key}'] = { "Seller": sell_addr, "Buyer": buyer, "Amount": round(amount/1000000), "Asset amount": "{}, You will get {}".format(qty + _fee_qty, qty), "Fee": "{} Algos + {} {}".format(_fee_qty, flat_fee, asset_name ) } if wtf: update.message.reply_text('Trade successfully initiated\nBuyer should proceed to approve the trade.') context.user_data.clear() else: return update.message.reply_text("Asset not found.") return ConversationHandler.conversation_timeout
# Sign the logic signature with an account sk lsig.sign(sk) # Get suggested parameters params = algod_client.suggested_params() # Comment out the next two (2) lines to use suggested fees # params.flat_fee = True # params.fee = 1000 # Build transaction amount = 10000 closeremainderto = None # Create a transaction txn = PaymentTxn(addr, params, receiver, amount, closeremainderto) # Create the LogicSigTransaction with contract account LogicSig lstx = transaction.LogicSigTransaction(txn, lsig) txns = [lstx] transaction.write_to_file(txns, "simple.stxn") # Send raw LogicSigTransaction to network txid = algod_client.send_transaction(lstx) print("Transaction ID: " + txid) confirmed_txn = wait_for_confirmation(algod_client, txid, 4) print("TXID: ", txid) print("Result confirmed in round: {}".format( confirmed_txn['confirmed-round'])) except Exception as e: print(e)