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,91 +1,120 @@
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/'
# CONTRACT_ADDRESS = '0xB352bBbe946f0b963F1e045Aa25d5606E13d21BF' # CONTRACT_ADDRESS = '0xB352bBbe946f0b963F1e045Aa25d5606E13d21BF'
# CHAIN_ID = 80002 # CHAIN_ID = 80002
# 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" } ]'
# # mainnet # # mainnet
# RPC = 'https://polygon-rpc.com' # RPC = 'https://polygon-rpc.com'
RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e" RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"
CONTRACT_ADDRESS = '0x552453F2C4e8ED860a6AEC7D6Bde143CF86E396a' 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 contractInst = web3.eth.contract(address=CAddress, abi=abi)
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(PRIVATE_KEY).address
acc1 = web3.eth.account.from_key(privatekey).address def send_transaction( gas_price):
def send_transaction(): nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions
nonce = web3.eth.get_transaction_count(acc1) uploadData = contractInst.functions.UploadConversionData(pubkey, user_id, project, data).build_transaction({
gas = 300000 'gasPrice': gas_price,
uploadData = contractInst.functions.UploadConversionData(pubkey,user_id,project,data).build_transaction({ 'chainId': CHAIN_ID,
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) 'from': acc1,
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) 'nonce': nonce
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) })
print("transaction hash",transaction_hash) signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY)
return transaction_hash transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
max_retries = 3 print("Transaction hash:", transaction_hash.hex())
retries = 0 return transaction_hash
transaction_hash = send_transaction() try:
acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
while retries < max_retries: initial_gas_price = web3.eth.gas_price
try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) max_retries = 4
print("Transaction confirmed:") retries = 0
break
except TimeExhausted: transaction_hash = None
retries += 1 try:
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") transaction_hash = send_transaction(initial_gas_price)
transaction_hash = send_transaction() except ValueError as e:
else: # If the error is due to "replacement transaction underpriced", handle it
print("Failed to confirm the transaction after multiple attempts.") if "replacement transaction underpriced" in str(e):
return TimeoutError print("Initial transaction attempt failed due to 'replacement transaction underpriced'. 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) elif "max fee per gas less than block base fee" in str(e):
except Exception as e: print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.")
print({"error":str(e)}) retries += 1
return e new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
def getConversion(private_key,user_id,project): else:
ACCOUNT = web3.eth.account.from_key(private_key).address raise
try:
getData = contractInst.functions.getConversion(user_id,project).call({'from':ACCOUNT}) while retries < max_retries and transaction_hash:
return True ,getData try:
except Exception as e: transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120)
print("Error calling function:", e) print("Transaction confirmed:")
return False,e break
except TimeExhausted:
def scriptGetUserprojectIds(user_id): retries += 1
try: print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
getProjectId = contractInst.functions.getProjectId(user_id).call() # Increase the gas price by 10% for each retry
return getProjectId new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
except Exception as e: transaction_hash = send_transaction(new_gas_price)
return e else:
if retries == max_retries:
def deleteConversion(privatekey,pubkey,user_id,project): print("Failed to confirm the transaction after multiple attempts.")
try: raise TimeoutError("Failed to confirm the transaction after multiple attempts.")
acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.getTransactionCount(acc1) transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed
deleteData = contractInst.functions.deleteConversion(pubkey,user_id,project).buildTransaction({ tx_id = transaction_hash.hex()
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) return tx_id, str(transaction_fee)
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) except Exception as e:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) print(f"An error occurred: {e}")
tx_id = transaction_hash.hex() raise
return tx_id except Exception as e:
except Exception as e: print({"error":str(e)})
print({"conversion_delete_error":str(e)}) return e
def getConversion(private_key,user_id,project):
ACCOUNT = web3.eth.account.from_key(private_key).address
try:
getData = contractInst.functions.getConversion(user_id,project).call({'from':ACCOUNT})
return True ,getData
except Exception as e:
print("Error calling function:", e)
return False,e
def scriptGetUserprojectIds(user_id):
try:
getProjectId = contractInst.functions.getProjectId(user_id).call()
return getProjectId
except Exception as e:
return e
def deleteConversion(privatekey,pubkey,user_id,project):
try:
acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.getTransactionCount(acc1)
deleteData = contractInst.functions.deleteConversion(pubkey,user_id,project).buildTransaction({
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce})
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
tx_id = transaction_hash.hex()
return tx_id
except Exception as e:
print({"conversion_delete_error":str(e)})

View File

@ -1,86 +1,115 @@
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/'
# CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881' # RPC = 'https://rpc-amoy.polygon.technology/'
# CHAIN_ID = 80002 # CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881'
# 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" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadScriptAuditData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptAudit", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptAudit", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptAuditDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" } ]' # CHAIN_ID = 80002
# 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" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadScriptAuditData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptAudit", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptAudit", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptAuditDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" } ]'
# mainnet
RPC = 'https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e' # mainnet
CONTRACT_ADDRESS = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c' RPC = 'https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e'
CHAIN_ID = 137 CONTRACT_ADDRESS = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c'
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" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadScriptAuditData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptAudit", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptAudit", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptAuditDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" } ]' 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" }, { "internalType": "string", "name": "_data", "type": "string" } ], "name": "UploadScriptAuditData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptAudit", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptAudit", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptAuditDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" } ]'
web3 = Web3(Web3.HTTPProvider(RPC))
CAddress =CONTRACT_ADDRESS web3 = Web3(Web3.HTTPProvider(RPC))
abi = ABI CAddress =CONTRACT_ADDRESS
contractInst = web3.eth.contract(address=CAddress, abi=abi) abi = ABI
contractInst = web3.eth.contract(address=CAddress, abi=abi)
def UploadScriptAuditData(privatekey,pubkey,user_id,project,data):
try: def UploadScriptAuditData(privatekey,pubkey,user_id,project,data):
acc1 = web3.eth.account.from_key(privatekey).address try:
def send_transaction(): acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
nonce = web3.eth.get_transaction_count(acc1) def send_transaction( gas_price):
uploadData = contractInst.functions.UploadScriptAuditData(pubkey,user_id,project,data).build_transaction({ nonce = web3.eth.get_transaction_count(acc1,'pending') # Get nonce including pending transactions
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) uploadData = contractInst.functions.UploadScriptAuditData(pubkey, user_id, project, data).build_transaction({
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) 'gasPrice': gas_price,
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) 'chainId': CHAIN_ID,
print("transaction hash",transaction_hash) 'from': acc1,
return transaction_hash 'nonce': nonce
max_retries = 3 })
retries = 0 signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=PRIVATE_KEY)
transaction_hash = send_transaction() transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print("Transaction hash:", transaction_hash.hex())
while retries < max_retries: return transaction_hash
try: acc1 = web3.eth.account.from_key(PRIVATE_KEY).address
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) initial_gas_price = web3.eth.gas_price
print("Transaction confirmed:")
break max_retries = 4
except TimeExhausted: retries = 0
retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") transaction_hash = None
transaction_hash = send_transaction() try:
else: transaction_hash = send_transaction(initial_gas_price)
print("Failed to confirm the transaction after multiple attempts.") except ValueError as e:
return TimeoutError # If the error is due to "replacement transaction underpriced", handle it
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed if "replacement transaction underpriced" in str(e):
tx_id = transaction_hash.hex() print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.")
return tx_id,str(trancation_fee) retries += 1
except Exception as e: new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
# print({"error":str(e)}) transaction_hash = send_transaction(new_gas_price)
return e 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.")
def getScriptAudit(private_key,user_id,project): retries += 1
ACCOUNT = web3.eth.account.from_key(private_key).address new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
try: transaction_hash = send_transaction(new_gas_price)
getData = contractInst.functions.getScriptAudit(user_id,project).call({'from':ACCOUNT}) else:
print(getData) raise
return True ,getData
except Exception as e: while retries < max_retries and transaction_hash:
print("Error calling function:", e) try:
return False,e transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120)
print("Transaction confirmed:")
def getUserprojectIds(user_id): break
try: except TimeExhausted:
getProjectId = contractInst.functions.getProjectId(user_id).call() retries += 1
return getProjectId print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
except Exception as e: # Increase the gas price by 10% for each retry
return e new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
transaction_hash = send_transaction(new_gas_price)
def deleteScriptAudit(privatekey,user_id,project): else:
try: if retries == max_retries:
acc1 = web3.eth.account.from_key(privatekey).address print("Failed to confirm the transaction after multiple attempts.")
nonce = web3.eth.getTransactionCount(acc1) raise TimeoutError("Failed to confirm the transaction after multiple attempts.")
deleteData = contractInst.functions.deleteScriptAudit(user_id,project).buildTransaction({
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey) tx_id = transaction_hash.hex()
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) return tx_id, str(transaction_fee)
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) except Exception as e:
tx_id = transaction_hash.hex() print({"error":str(e)})
return tx_id return e
except Exception as e:
print({"PPT_conversion_delete_error":str(e)}) def getScriptAudit(private_key,user_id,project):
ACCOUNT = web3.eth.account.from_key(private_key).address
try:
getData = contractInst.functions.getScriptAudit(user_id,project).call({'from':ACCOUNT})
print(getData)
return True ,getData
except Exception as e:
print("Error calling function:", e)
return False,e
def getUserprojectIds(user_id):
try:
getProjectId = contractInst.functions.getProjectId(user_id).call()
return getProjectId
except Exception as e:
return e
def deleteScriptAudit(privatekey,user_id,project):
try:
acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.getTransactionCount(acc1)
deleteData = contractInst.functions.deleteScriptAudit(user_id,project).buildTransaction({
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce})
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
tx_id = transaction_hash.hex()
return tx_id
except Exception as e:
print({"PPT_conversion_delete_error":str(e)})

View File

@ -1,96 +1,122 @@
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/'
# CONTRACT_ADDRESS = '0x46411D9d30a56F5BDD0c30595b2f91701884EBE9' # CONTRACT_ADDRESS = '0x46411D9d30a56F5BDD0c30595b2f91701884EBE9'
# CHAIN_ID = 80002 # CHAIN_ID = 80002
# 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": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptPad", "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": "UploadScriptPadData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptPad", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptPadDataBase.privateData", "name": "", "type": "tuple" } ], "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": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptPad", "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": "UploadScriptPadData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptPad", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptPadDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" } ]'
# mainnet # mainnet
# RPC = 'https://polygon-rpc.com' # RPC = 'https://polygon-rpc.com'
RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e" RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"
CONTRACT_ADDRESS = '0xa3eE891d2b0bE5bcc99f14AFF37b42161Cae8E7B' CONTRACT_ADDRESS = '0xa3eE891d2b0bE5bcc99f14AFF37b42161Cae8E7B'
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": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptPad", "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": "UploadScriptPadData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptPad", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptPadDataBase.privateData", "name": "", "type": "tuple" } ], "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": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteScriptPad", "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": "UploadScriptPadData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "userId", "type": "uint256" } ], "name": "getProjectId", "outputs": [ { "internalType": "string[]", "name": "", "type": "string[]" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getScriptPad", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct ScriptPadDataBase.privateData", "name": "", "type": "tuple" } ], "stateMutability": "view", "type": "function" } ]'
web3 = Web3(Web3.HTTPProvider(RPC)) web3 = Web3(Web3.HTTPProvider(RPC))
CAddress = CONTRACT_ADDRESS CAddress = CONTRACT_ADDRESS
abi = ABI abi = ABI
contractInst = web3.eth.contract(address=CAddress, abi=abi) 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,
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) 'from': acc1,
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) 'nonce': nonce
print(web3.eth.gas_price) })
print("transaction hash",transaction_hash) signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey)
return transaction_hash transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
# Define the number of retries print("Transaction hash:", transaction_hash.hex())
max_retries = 3 return transaction_hash
retries = 0 try:
transaction_hash = send_transaction() acc1 = web3.eth.account.from_key(privatekey).address
initial_gas_price = web3.eth.gas_price
while retries < max_retries:
try: max_retries = 4
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash,timeout=60) retries = 0
print("Transaction confirmed:")
break transaction_hash = None
except TimeExhausted: try:
retries += 1 transaction_hash = send_transaction(initial_gas_price)
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})") except ValueError as e:
transaction_hash = send_transaction() # If the error is due to "replacement transaction underpriced", handle it
else: if "replacement transaction underpriced" in str(e):
print("Failed to confirm the transaction after multiple attempts.") print("Initial transaction attempt failed due to 'replacement transaction underpriced'. Retrying with higher gas price.")
return TimeoutError 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)
print("tx_id") elif "max fee per gas less than block base fee" in str(e):
return tx_id,str(trancation_fee) print("Initial transaction attempt failed due to 'max fee per gas less than block base fee'. Retrying with higher gas price.")
except Exception as e: retries += 1
# print({"error":str(e)}) new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
return e transaction_hash = send_transaction(new_gas_price)
else:
def getScriptPad(private_key,user_id,project): raise
ACCOUNT = web3.eth.account.from_key(private_key).address
try: while retries < max_retries and transaction_hash:
getData = contractInst.functions.getScriptPad(user_id,project).call({'from':ACCOUNT}) try:
print(getData) transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash, timeout=120)
return True ,getData print("Transaction confirmed:")
except Exception as e: break
print("Error calling function:", e) except TimeExhausted:
return False,e retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
def getUserprojectIds(user_id): # Increase the gas price by 10% for each retry
try: new_gas_price = int(web3.eth.gas_price * (1 + 0.1 * retries))
getProjectId = contractInst.functions.getProjectId(user_id).call() transaction_hash = send_transaction(new_gas_price)
return getProjectId else:
except Exception as e: if retries == max_retries:
return e print("Failed to confirm the transaction after multiple attempts.")
raise TimeoutError("Failed to confirm the transaction after multiple attempts.")
def deleteScriptPad(privatekey,user_id,project):
try: transaction_fee = transaction_receipt.effectiveGasPrice * transaction_receipt.gasUsed
acc1 = web3.eth.account.from_key(privatekey).address tx_id = transaction_hash.hex()
nonce = web3.eth.getTransactionCount(acc1) return tx_id, str(transaction_fee)
deleteData = contractInst.functions.deleteScriptPad(user_id,project).buildTransaction({
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) except Exception as e:
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey) print(f"An error occurred: {e}")
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) raise
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) except Exception as e:
tx_id = transaction_hash.hex() print({"error":str(e)})
return tx_id return e
except Exception as e:
print({"PPT_conversion_delete_error":str(e)}) def getScriptPad(private_key,user_id,project):
ACCOUNT = web3.eth.account.from_key(private_key).address
# script = UploadScriptPadData("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",'0xd7F252B08B19e35344ac44DE7CCdd26D10Cc6e17',1,'1752aa50-a751-4149-84ad-680dcb932972',"pravesh11") try:
# print(script) getData = contractInst.functions.getScriptPad(user_id,project).call({'from':ACCOUNT})
# getScriptPad("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",1,'1752aa50-a751-4149-84ad-680dcb932972') print(getData)
return True ,getData
except Exception as e:
print("Error calling function:", e)
return False,e
def getUserprojectIds(user_id):
try:
getProjectId = contractInst.functions.getProjectId(user_id).call()
return getProjectId
except Exception as e:
return e
def deleteScriptPad(privatekey,user_id,project):
try:
acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.getTransactionCount(acc1)
deleteData = contractInst.functions.deleteScriptPad(user_id,project).buildTransaction({
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce})
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
tx_id = transaction_hash.hex()
return tx_id
except Exception as e:
print({"PPT_conversion_delete_error":str(e)})