-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_funds.py
More file actions
70 lines (53 loc) · 2.63 KB
/
send_funds.py
File metadata and controls
70 lines (53 loc) · 2.63 KB
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
from algosdk import account, mnemonic
from algosdk.transaction import PaymentTxn, wait_for_confirmation
from algosdk.v2client import algod
import random
import base64
import time
def wait_for_confirmation_with_timeout(client, txid, timeout=1000):
start_time = time.time()
while True:
elapsed_time = time.time() - start_time
if elapsed_time > timeout:
print(f"Transaction confirmation timed out after {timeout} seconds")
return None
# Check the transaction status
try:
pending_txn = client.pending_transaction_info(txid)
except Exception as e:
print(f"Error fetching pending transaction: {e}")
return None
if 'confirmed-round' in pending_txn:
return pending_txn['confirmed-round']
time.sleep(1) # Wait for 1 second before retrying
def send_funds(private_key, to_address, amount, node_address, node_token):
algod_client = algod.AlgodClient(node_token, node_address)
public_key = account.address_from_private_key(private_key)
print(f"Public Key: {public_key}")
print(f"Receiver's Address: {to_address}")
account_info = algod_client.account_info(public_key)
print(f"Sender's balance before transaction: {account_info['amount']} microAlgos")
params = algod_client.suggested_params()
print(f"Suggested Params: {params}")
random_note = random.randint(1, 10000) # Generating a random integer as the note
encoded_note = base64.b64encode(str(random_note).encode())
txn = PaymentTxn(public_key, params, to_address, amount, None, note=encoded_note)
signed_txn = txn.sign(private_key)
txid = algod_client.send_transaction(signed_txn)
print(f"Transaction ID: {txid}")
try:
confirmed_round = wait_for_confirmation_with_timeout(algod_client, txid)
if confirmed_round:
print(f"Transaction confirmed in round {confirmed_round}")
except Exception as e:
print(f"Exception raised: {e}")
def main():
example_private_key = "UK790krMFIp90Z02KuuLk+g6O5GOnQwSBYyqqMCw/w/z03/UuY2YCWL3xuu8RXC13ybK5QauZ+2hkgh+ZM2y/A=="
example_to_address = "VGNJEZSWN66IXDE7FC3LFFCPGEP3DGHX5DHBUK662JDTS7OZ2SPF7CVKAI" # "P4IUIPHOHJTWFKMYYKZCBY3B6CVZ5RASWXHSFTJK2UYIGCVOLZ7NUCBJOA"
example_amount = 2000000000
example_node_address = "http://192.168.30.2:4100" # "http://192.168.30.5:4100"
example_node_token = "97361fdc801fe9fd7f2ae87fa4ea5dc8b9b6ce7380c230eaf5494c4cb5d38d61"
# Call the send_funds function
send_funds(example_private_key, example_to_address, example_amount, example_node_address, example_node_token)
if __name__ == "__main__":
main()