Blockchain changes with error of non-iterable ValueError errror resolved

This commit is contained in:
Ubuntu 2024-06-24 06:14:40 +00:00
parent 291510922e
commit e6fa5b8293
3 changed files with 357 additions and 273 deletions

View File

@ -1,7 +1,7 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted from web3.exceptions import TimeExhausted,TransactionNotFound
PRIVATE_KEY = "2c7c09807b78128e00a2ed308c122a0ecc45847ffe0195eca4b4d2463fb11549"
# test network # test network
# RPC = 'https://rpc-amoy.polygon.technology/' # RPC = 'https://rpc-amoy.polygon.technology/'
@ -16,7 +16,6 @@ CONTRACT_ADDRESS = '0x552453F2C4e8ED860a6AEC7D6Bde143CF86E396a'
CHAIN_ID = 137 CHAIN_ID = 137
ABI = '[ { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "indexed": false, "internalType": "string", "name": "project", "type": "string" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "conversion", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "pubkey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteConversion", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "pubKey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadConversionData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getConversion", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct scriptConversionDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" } ]' ABI = '[ { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": false, "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "indexed": false, "internalType": "string", "name": "project", "type": "string" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" } ], "name": "conversion", "type": "event" }, { "inputs": [ { "internalType": "address", "name": "pubkey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteConversion", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "pubKey", "type": "address" }, { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadConversionData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getConversion", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct scriptConversionDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" } ]'
web3 = Web3(Web3.HTTPProvider(RPC)) web3 = Web3(Web3.HTTPProvider(RPC))
CAddress = CONTRACT_ADDRESS CAddress = CONTRACT_ADDRESS
abi = ABI abi = ABI
@ -24,36 +23,67 @@ contractInst = web3.eth.contract(address=CAddress, abi=abi)
def UploadConversionData(privatekey,pubkey, user_id,project,data): def UploadConversionData(privatekey,pubkey, user_id,project,data):
try: try:
acc1 = web3.eth.account.from_key(privatekey).address acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
def send_transaction(): def send_transaction( gas_price):
nonce = web3.eth.get_transaction_count(acc1) nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions
gas = 300000 uploadData = contractInst.functions.UploadConversionData(pubkey, user_id, project, data).build_transaction({
uploadData = contractInst.functions.UploadConversionData(pubkey,user_id,project,data).build_transaction({ 'gasPrice': gas_price,
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) 'chainId': CHAIN_ID,
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) 'from': acc1,
'nonce': nonce
})
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print("transaction hash",transaction_hash) print("Transaction hash:", transaction_hash.hex())
return transaction_hash return transaction_hash
max_retries = 3 try:
retries = 0 acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
transaction_hash = send_transaction() initial_gas_price = web3.eth.gas_price
while retries < max_retries: max_retries = 4
retries = 0
transaction_hash = None
try: try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) transaction_hash = send_transaction(initial_gas_price)
print("Transaction confirmed:") except ValueError as e:
break # If the error is due to "replacement transaction underpriced", handle it
except TimeExhausted: if "replacement transaction underpriced" in str(e):
retries += 1 print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.")
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") retries += 1
transaction_hash = send_transaction() new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
else: transaction_hash = send_transaction(new_gas_price)
print("Failed to confirm the transaction after multiple attempts.") elif "max fee per gas less than block base fee" in str(e):
return TimeoutError print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.")
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) retries += 1
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
tx_id = transaction_hash.hex() transaction_hash = send_transaction(new_gas_price)
return tx_id,str(trancation_fee) else:
raise
while retries < max_retries and transaction_hash:
try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120)
print("Transaction confirmed:")
break
except TimeExhausted:
retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
# Increase the gas price by 10% for each retry
new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
else:
if retries == max_retries:
print("Failed to confirm the transaction after multiple attempts.")
raise TimeoutError("Failed to confirm the transaction after multiple attempts.")
transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed
tx_id = transaction_hash.hex()
return tx_id, str(transaction_fee)
except Exception as e:
print(f"An error occurred: {e}")
raise
except Exception as e: except Exception as e:
print({"error":str(e)}) print({"error":str(e)})
return e return e
@ -88,4 +118,3 @@ def deleteConversion(privatekey,pubkey,user_id,project):
except Exception as e: except Exception as e:
print({"conversion_delete_error":str(e)}) print({"conversion_delete_error":str(e)})

View File

@ -1,6 +1,7 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted from web3.exceptions import TimeExhausted,TransactionNotFound
PRIVATE_KEY = "a632f28406579d96879cfc49d55411bb1424dbbae60bf4dd93ad0710b63d6b80"
# RPC = 'https://rpc-amoy.polygon.technology/' # RPC = 'https://rpc-amoy.polygon.technology/'
# CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881' # CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881'
@ -20,36 +21,64 @@ contractInst = web3.eth.contract(address=CAddress, abi=abi)
def UploadScriptAuditData(privatekey,pubkey,user_id,project,data): def UploadScriptAuditData(privatekey,pubkey,user_id,project,data):
try: try:
acc1 = web3.eth.account.from_key(privatekey).address acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
def send_transaction(): def send_transaction( gas_price):
nonce = web3.eth.get_transaction_count(acc1) nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions
uploadData = contractInst.functions.UploadScriptAuditData(pubkey,user_id,project,data).build_transaction({ uploadData = contractInst.functions.UploadScriptAuditData(pubkey, user_id, project, data).build_transaction({
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) 'gasPrice': gas_price,
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) 'chainId': CHAIN_ID,
'from': acc1,
'nonce': nonce
})
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print("transaction hash",transaction_hash) print("Transaction hash:", transaction_hash.hex())
return transaction_hash return transaction_hash
max_retries = 3 acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
retries = 0 initial_gas_price = web3.eth.gas_price
transaction_hash = send_transaction()
while retries < max_retries: max_retries = 4
retries = 0
transaction_hash = None
try:
transaction_hash = send_transaction(initial_gas_price)
except ValueError as e:
# If the error is due to "replacement transaction underpriced", handle it
if "replacement transaction underpriced" in str(e):
print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.")
retries += 1
new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
elif "max fee per gas less than block base fee" in str(e):
print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.")
retries += 1
new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
else:
raise
while retries < max_retries and transaction_hash:
try: try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120)
print("Transaction confirmed:") print("Transaction confirmed:")
break break
except TimeExhausted: except TimeExhausted:
retries += 1 retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
transaction_hash = send_transaction() # Increase the gas price by 10% for each retry
new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
else: else:
print("Failed to confirm the transaction after multiple attempts.") if retries == max_retries:
return TimeoutError print("Failed to confirm the transaction after multiple attempts.")
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed raise TimeoutError("Failed to confirm the transaction after multiple attempts.")
transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed
tx_id = transaction_hash.hex() tx_id = transaction_hash.hex()
return tx_id,str(trancation_fee) return tx_id, str(transaction_fee)
except Exception as e: except Exception as e:
# print({"error":str(e)}) print({"error":str(e)})
return e return e
def getScriptAudit(private_key,user_id,project): def getScriptAudit(private_key,user_id,project):

View File

@ -1,6 +1,6 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted from web3.exceptions import TimeExhausted, TransactionNotFound
# testnetwork # testnetwork
# RPC = 'https://rpc-amoy.polygon.technology/' # RPC = 'https://rpc-amoy.polygon.technology/'
@ -25,39 +25,68 @@ contractInst = web3.eth.contract(address=CAddress, abi=abi)
def UploadScriptPadData(privatekey,pubkey,user_id,project,data): def UploadScriptPadData(privatekey,pubkey,user_id,project,data):
try: try:
acc1 = web3.eth.account.from_key(privatekey).address acc1 = web3.eth.account.from_key(privatekey).address
def send_transaction(): def send_transaction(gas_price):
nonce = web3.eth.get_transaction_count(acc1) nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions
gas = 300000 uploadData = contractInst.functions.UploadScriptPadData(pubkey, user_id, project, data).build_transaction({
uploadData = contractInst.functions.UploadScriptPadData(pubkey,user_id,project,data).build_transaction({ 'gasPrice': gas_price,
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) 'chainId': CHAIN_ID,
'from': acc1,
'nonce': nonce
})
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print(web3.eth.gas_price) print("Transaction hash:", transaction_hash.hex())
print("transaction hash",transaction_hash)
return transaction_hash return transaction_hash
# Define the number of retries try:
max_retries = 3 acc1 = web3.eth.account.from_key(privatekey).address
retries = 0 initial_gas_price = web3.eth.gas_price
transaction_hash = send_transaction()
while retries < max_retries: max_retries = 4
retries = 0
transaction_hash = None
try: try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash,timeout=60) transaction_hash = send_transaction(initial_gas_price)
print("Transaction confirmed:") except ValueError as e:
break # If the error is due to "replacement transaction underpriced", handle it
except TimeExhausted: if "replacement transaction underpriced" in str(e):
retries += 1 print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.")
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") retries += 1
transaction_hash = send_transaction() new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
else: transaction_hash = send_transaction(new_gas_price)
print("Failed to confirm the transaction after multiple attempts.") elif "max fee per gas less than block base fee" in str(e):
return TimeoutError print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.")
trancation_fee= transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed retries += 1
tx_id = transaction_hash.hex() new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
print("tx_id") transaction_hash = send_transaction(new_gas_price)
return tx_id,str(trancation_fee) else:
raise
while retries < max_retries and transaction_hash:
try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120)
print("Transaction confirmed:")
break
except TimeExhausted:
retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
# Increase the gas price by 10% for each retry
new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
else:
if retries == max_retries:
print("Failed to confirm the transaction after multiple attempts.")
raise TimeoutError("Failed to confirm the transaction after multiple attempts.")
transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed
tx_id = transaction_hash.hex()
return tx_id, str(transaction_fee)
except Exception as e:
print(f"An error occurred: {e}")
raise
except Exception as e: except Exception as e:
# print({"error":str(e)}) print({"error":str(e)})
return e return e
def getScriptPad(private_key,user_id,project): def getScriptPad(private_key,user_id,project):
@ -91,6 +120,3 @@ def deleteScriptPad(privatekey,user_id,project):
except Exception as e: except Exception as e:
print({"PPT_conversion_delete_error":str(e)}) print({"PPT_conversion_delete_error":str(e)})
# script = UploadScriptPadData("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",'0xd7F252B08B19e35344ac44DE7CCdd26D10Cc6e17',1,'1752aa50-a751-4149-84ad-680dcb932972',"pravesh11")
# print(script)
# getScriptPad("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",1,'1752aa50-a751-4149-84ad-680dcb932972')