Error in sending future transactions with Algorand Python SDK - General - Algorand
Error in sending future transactions with Algorand Python SDK
post by Obi on Sep 29, 2021
I am trying to send a transaction 20 minutes into the future but I end up with this error
Traceback (most recent call last):
File "C:\Users\0xEarl\anaconda3\lib\site-packages\algosdk\v2client\algod.py", line 73, in algod_request
resp = urlopen(req)
File "C:\Users\0xEarl\anaconda3\lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "C:\Users\0xEarl\anaconda3\lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "C:\Users\0xEarl\anaconda3\lib\urllib\request.py", line 640, in http_response
response = self.parent.error(
File "C:\Users\0xEarl\anaconda3\lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "C:\Users\0xEarl\anaconda3\lib\urllib\request.py", line 502, in _call_chain
result = func(*args)
File "C:\Users\0xEarl\anaconda3\lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\0xEarl\anaconda3\lib\site-packages\algosdk\v2client\algod.py", line 78, in algod_request
raise error.AlgodHTTPError(json.loads(e)["message"], code)
algosdk.error.AlgodHTTPError: TransactionPool.Remember: txn dead: round 16527277 outside of 16527542--16528542
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:/Users/0xEarl/Desktop/Learn_Algo/futuretx.py", line 41, in <module>
txid = algod_client.send_transaction(signed_txn) # send the transaction
File "C:\Users\0xEarl\anaconda3\lib\site-packages\algosdk\v2client\algod.py", line 195, in send_transaction
return self.send_raw_transaction(encoding.msgpack_encode(txn),
File "C:\Users\0xEarl\anaconda3\lib\site-packages\algosdk\v2client\algod.py", line 214, in send_raw_transaction
return self.algod_request("POST", req, data=txn, **kwargs)["txId"]
File "C:\Users\0xEarl\anaconda3\lib\site-packages\algosdk\v2client\algod.py", line 80, in algod_request
raise error.AlgodHTTPError(e, code)
algosdk.error.AlgodHTTPError: {"message":"TransactionPool.Remember: txn dead: round 16527277 outside of 16527542--16528542"}
post by fabrice on Sep 29, 2021
Welcome to Algorand!
The node will only accept the transaction when the current round is in the validity window. You need to have a service/script that waits until the moment the current round passes the first valid round of the transaction, and then only send the transaction.
post by Obi on Sep 30, 2021
Hello @fabrice i have attached the code for your review, thanks.
import json
from algosdk.v2client import algod
from algosdk import transaction
algod_address = "https://mainnet-algorand.api.purestake.io/ps2" # network address
algod_token = "π" # api key
headers = {
"X-API-Key": algod_token,
}
# initiate a new client with the given parameters
algod_client = algod.AlgodClient(algod_token, algod_address, headers)
params = algod_client.suggested_params()
params.flat_fee = True
params.fee = 1000
fee = params.fee
gh = params.gh
# check node status
status = algod_client.status()
# to read json data
node_stat = json.dumps(status, indent=4)
node_stat = json.loads(node_stat)
current_round = int(node_stat["last-round"])
avg_block_time = 4.5
target_submission_time_in_seconds = 1200
avg_number_of_block_produced_within_time = int(target_submission_time_in_seconds/avg_block_time)
first_round = int(current_round + avg_number_of_block_produced_within_time)
last_round = int(first_round + 1000)
sender = 'I76WK5LC6JDKOIQ4AZ2BKGENET37AUR57V4NRKKDY3UARDTJ534LGPPVCE'
sender_key = 'π'
reciever = '7WEP4COP42SO6XPCIUIEXYPBYWL2K3QNEV73FPNSU5WC26JYTXMNSC6QJI'
amount = 100000
payit = transaction.PaymentTxn(sender, fee, first_round, last_round, gh, reciever, amount)
signed_txn = payit.sign(sender_key)
# submit the transaction
txid = algod_client.send_transaction(signed_txn) # send the transaction
print(f"Successfully sent transaction with txID: {txid}") # prints the transaction id
post by fabrice on Sep 30, 2021
In this specific case, the issue is that
post by Obi on Sep 30, 2021
Hello Fabrice, i have fixed the issue thanks
post by TheD on Oct 16, 2021
Hello Obi, Can you please share the code with the solution. Thank you
post by Obi on Feb 10, 2022
def send_future_algo(time_in_milli_seconds: int, sender_addr: str, receiver: str, amount: int, sender_key: str) -> dict:
"""send timed algo transactions by the networks standards, but this is crap"""
transactioninfo = {}
params = algod_client.suggested_params()
status = algod_client.status()
current_round = status['last-round']
avg_blocktime = 4.5
avg_blocks_produced = time_in_milli_seconds/avg_blocktime
first_valid_round = int(current_round + avg_blocks_produced)
last_valid_round = int(first_valid_round + 1000)
params.flat_fee = True
params.fee = 1000
params.first = first_valid_round
params.last = last_valid_round
while 1 > 0:
status = algod_client.status()
current_round = int(status['last-round'])
if current_round > first_valid_round:
txn = PaymentTxn(sender_addr, params, receiver, amount)
signed_txn = txn.sign(sender_key)
txid = algod_client.send_transaction(signed_txn) # send the transaction
transactioninfo['txid'] = txid
transactioninfo['link'] = f"https://testnet.algoexplorer.io/tx/{txid}"
break
return transactioninfo