Blockchain changes

This commit is contained in:
Ubuntu 2024-06-19 07:50:55 +00:00
parent 645033a26e
commit 972206cc2c
3 changed files with 274 additions and 212 deletions

View File

@ -1,72 +1,91 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted
# test network
# test network
# RPC = 'https://rpc-amoy.polygon.technology/'
# CONTRACT_ADDRESS = '0xB352bBbe946f0b963F1e045Aa25d5606E13d21BF' # RPC = 'https://rpc-amoy.polygon.technology/'
# CHAIN_ID = 80002 # CONTRACT_ADDRESS = '0xB352bBbe946f0b963F1e045Aa25d5606E13d21BF'
# 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" } ]' # 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" } ]'
# # mainnet
RPC = 'https://polygon-rpc.com' # # mainnet
CONTRACT_ADDRESS = '0x552453F2C4e8ED860a6AEC7D6Bde143CF86E396a' # RPC = 'https://polygon-rpc.com'
CHAIN_ID = 137 RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"
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" } ]' CONTRACT_ADDRESS = '0x552453F2C4e8ED860a6AEC7D6Bde143CF86E396a'
CHAIN_ID = 137
# web3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e")) 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("https://rpc-mumbai.maticvigil.com/"))
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 UploadConversionData(privatekey,pubkey, user_id,project,data):
try: def UploadConversionData(privatekey,pubkey, user_id,project,data):
print("private Key",privatekey) try:
acc1 = web3.eth.account.from_key(privatekey).address acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.get_transaction_count(acc1) def send_transaction():
uploadData = contractInst.functions.UploadConversionData(pubkey,user_id,project,data).build_transaction({ nonce = web3.eth.get_transaction_count(acc1)
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) gas = 300000
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) uploadData = contractInst.functions.UploadConversionData(pubkey,user_id,project,data).build_transaction({
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) 'gas': gas ,'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce})
print("transaction hash",transaction_hash) signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey)
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed print("transaction hash",transaction_hash)
tx_id = transaction_hash.hex() return transaction_hash
return tx_id,str(trancation_fee) max_retries = 3
except Exception as e: retries = 0
print({"error":str(e)}) transaction_hash = send_transaction()
return e
while retries < max_retries:
def getConversion(private_key,user_id,project): try:
ACCOUNT = web3.eth.account.from_key(private_key).address transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
try: print("Transaction confirmed:")
getData = contractInst.functions.getConversion(user_id,project).call({'from':ACCOUNT}) break
return True ,getData except TimeExhausted:
except Exception as e: retries += 1
print("Error calling function:", e) print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
return False,e transaction_hash = send_transaction()
else:
def scriptGetUserprojectIds(user_id): print("Failed to confirm the transaction after multiple attempts.")
try: return TimeoutError
getProjectId = contractInst.functions.getProjectId(user_id).call() transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
return getProjectId trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed
except Exception as e: tx_id = transaction_hash.hex()
return e return tx_id,str(trancation_fee)
except Exception as e:
def deleteConversion(privatekey,pubkey,user_id,project): print({"error":str(e)})
try: return e
acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.getTransactionCount(acc1) def getConversion(private_key,user_id,project):
deleteData = contractInst.functions.deleteConversion(pubkey,user_id,project).buildTransaction({ ACCOUNT = web3.eth.account.from_key(private_key).address
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) try:
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey) getData = contractInst.functions.getConversion(user_id,project).call({'from':ACCOUNT})
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) return True ,getData
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) except Exception as e:
tx_id = transaction_hash.hex() print("Error calling function:", e)
return tx_id return False,e
except Exception as e:
print({"conversion_delete_error":str(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,72 +1,87 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted
# RPC = 'https://rpc-amoy.polygon.technology/'
# RPC = 'https://rpc-amoy.polygon.technology/' # CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881'
# CONTRACT_ADDRESS = '0xbE91e2294D12fa78Ce64657fD9Ee137cE3e99881' # 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" }, { "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" } ]'
# 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
# web3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e")) RPC = 'https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e'
CONTRACT_ADDRESS = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c'
# mainnet CHAIN_ID = 137
RPC = 'https://polygon-rpc.com' 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" } ]'
CONTRACT_ADDRESS = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c'
CHAIN_ID = 137 web3 = Web3(Web3.HTTPProvider(RPC))
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" } ]' CAddress =CONTRACT_ADDRESS
abi = ABI
web3 = Web3(Web3.HTTPProvider(RPC)) contractInst = web3.eth.contract(address=CAddress, abi=abi)
CAddress =CONTRACT_ADDRESS
abi = ABI def UploadScriptAuditData(privatekey,pubkey,user_id,project,data):
contractInst = web3.eth.contract(address=CAddress, abi=abi) try:
acc1 = web3.eth.account.from_key(privatekey).address
def UploadScriptAuditData(privatekey,pubkey,user_id,project,data): def send_transaction():
try: nonce = web3.eth.get_transaction_count(acc1)
print("private Key",privatekey) gas = 300000
acc1 = web3.eth.account.from_key(privatekey).address uploadData = contractInst.functions.UploadScriptAuditData(pubkey,user_id,project,data).build_transaction({
nonce = web3.eth.get_transaction_count(acc1) 'gas': gas,'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': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) print("transaction hash",transaction_hash)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) return transaction_hash
print("transaction hash",transaction_hash) max_retries = 3
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) retries = 0
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed transaction_hash = send_transaction()
tx_id = transaction_hash.hex()
return tx_id,str(trancation_fee) while retries < max_retries:
except Exception as e: try:
# print({"error":str(e)}) transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
return e print("Transaction confirmed:")
break
def getScriptAudit(private_key,user_id,project): except TimeExhausted:
ACCOUNT = web3.eth.account.from_key(private_key).address retries += 1
try: print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
getData = contractInst.functions.getScriptAudit(user_id,project).call({'from':ACCOUNT}) transaction_hash = send_transaction()
print(getData) else:
return True ,getData print("Failed to confirm the transaction after multiple attempts.")
except Exception as e: return TimeoutError
print("Error calling function:", e) trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed
return False,e tx_id = transaction_hash.hex()
return tx_id,str(trancation_fee)
def getUserprojectIds(user_id): except Exception as e:
try: # print({"error":str(e)})
getProjectId = contractInst.functions.getProjectId(user_id).call() return e
return getProjectId
except Exception as e: def getScriptAudit(private_key,user_id,project):
return e ACCOUNT = web3.eth.account.from_key(private_key).address
try:
def deleteScriptAudit(privatekey,user_id,project): getData = contractInst.functions.getScriptAudit(user_id,project).call({'from':ACCOUNT})
try: print(getData)
acc1 = web3.eth.account.from_key(privatekey).address return True ,getData
nonce = web3.eth.getTransactionCount(acc1) except Exception as e:
deleteData = contractInst.functions.deleteScriptAudit(user_id,project).buildTransaction({ print("Error calling function:", e)
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) return False,e
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) def getUserprojectIds(user_id):
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) try:
tx_id = transaction_hash.hex() getProjectId = contractInst.functions.getProjectId(user_id).call()
return tx_id return getProjectId
except Exception as e: except Exception as e:
print({"PPT_conversion_delete_error":str(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,68 +1,96 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted
# web3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"))
RPC = 'https://rpc-amoy.polygon.technology/' # testnetwork
CONTRACT_ADDRESS = '0x46411D9d30a56F5BDD0c30595b2f91701884EBE9' # RPC = 'https://rpc-amoy.polygon.technology/'
CHAIN_ID = 80002 # CONTRACT_ADDRESS = '0x46411D9d30a56F5BDD0c30595b2f91701884EBE9'
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" } ]' # CHAIN_ID = 80002
web3 = Web3(Web3.HTTPProvider(RPC)) # 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
# CAddress = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c' # mainnet
# 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" } ]'
CAddress = CONTRACT_ADDRESS # RPC = 'https://polygon-rpc.com'
abi = ABI RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"
contractInst = web3.eth.contract(address=CAddress, abi=abi) CONTRACT_ADDRESS = '0xa3eE891d2b0bE5bcc99f14AFF37b42161Cae8E7B'
CHAIN_ID = 137
def UploadScriptPadData(privatekey,pubkey,user_id,project,data): 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" } ]'
try:
print("private Key",privatekey) web3 = Web3(Web3.HTTPProvider(RPC))
acc1 = web3.eth.account.from_key(privatekey).address
nonce = web3.eth.get_transaction_count(acc1) CAddress = CONTRACT_ADDRESS
uploadData = contractInst.functions.UploadScriptPadData(pubkey,user_id,project,data).build_transaction({ abi = ABI
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) contractInst = web3.eth.contract(address=CAddress, abi=abi)
signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) def UploadScriptPadData(privatekey,pubkey,user_id,project,data):
print("transaction hash",transaction_hash) try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) acc1 = web3.eth.account.from_key(privatekey).address
trancation_fee= transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed def send_transaction():
tx_id = transaction_hash.hex() nonce = web3.eth.get_transaction_count(acc1)
return tx_id,str(trancation_fee) gas = 300000
except Exception as e: uploadData = contractInst.functions.UploadScriptPadData(pubkey,user_id,project,data).build_transaction({
# print({"error":str(e)}) 'gas': gas , 'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce})
return e signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey)
transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
def getScriptPad(private_key,user_id,project): print(web3.eth.gas_price)
ACCOUNT = web3.eth.account.from_key(private_key).address print("transaction hash",transaction_hash)
try: return transaction_hash
getData = contractInst.functions.getScriptPad(user_id,project).call({'from':ACCOUNT}) # Define the number of retries
print(getData) max_retries = 3
return True ,getData retries = 0
except Exception as e: transaction_hash = send_transaction()
print("Error calling function:", e)
return False,e while retries < max_retries:
try:
def getUserprojectIds(user_id): transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash,timeout=60)
try: print("Transaction confirmed:")
getProjectId = contractInst.functions.getProjectId(user_id).call() break
return getProjectId except TimeExhausted:
except Exception as e: retries += 1
return e print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
transaction_hash = send_transaction()
def deleteScriptPad(privatekey,user_id,project): else:
try: print("Failed to confirm the transaction after multiple attempts.")
acc1 = web3.eth.account.from_key(privatekey).address return TimeoutError
nonce = web3.eth.getTransactionCount(acc1) trancation_fee= transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed
deleteData = contractInst.functions.deleteScriptPad(user_id,project).buildTransaction({ tx_id = transaction_hash.hex()
'gasPrice': web3.eth.gas_price, 'chainId': CHAIN_ID, 'from': acc1, 'nonce': nonce}) print("tx_id")
signed_transaction = web3.eth.account.sign_transaction(deleteData, private_key=privatekey) return tx_id,str(trancation_fee)
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({"error":str(e)})
tx_id = transaction_hash.hex() return e
return tx_id
except Exception as e: def getScriptPad(private_key,user_id,project):
print({"PPT_conversion_delete_error":str(e)}) ACCOUNT = web3.eth.account.from_key(private_key).address
try:
# script = UploadScriptPadData("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",'0xd7F252B08B19e35344ac44DE7CCdd26D10Cc6e17',1,'1752aa50-a751-4149-84ad-680dcb932972',"fhdsjkdfd") getData = contractInst.functions.getScriptPad(user_id,project).call({'from':ACCOUNT})
# print(script) print(getData)
# getScriptPad("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",1,'1752aa50-a751-4149-84ad-680dcb932972') 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)})
# script = UploadScriptPadData("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",'0xd7F252B08B19e35344ac44DE7CCdd26D10Cc6e17',1,'1752aa50-a751-4149-84ad-680dcb932972',"pravesh11")
# print(script)
# getScriptPad("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",1,'1752aa50-a751-4149-84ad-680dcb932972')