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,5 +1,6 @@
from web3 import Web3 from web3 import Web3
import time import time
from web3.exceptions import TimeExhausted
# test network # test network
@ -9,13 +10,13 @@ import time
# 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"
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("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"))
# web3 = Web3(Web3.HTTPProvider("https://rpc-mumbai.maticvigil.com/"))
web3 = Web3(Web3.HTTPProvider(RPC)) web3 = Web3(Web3.HTTPProvider(RPC))
CAddress = CONTRACT_ADDRESS CAddress = CONTRACT_ADDRESS
abi = ABI abi = ABI
@ -23,14 +24,32 @@ 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:
print("private Key",privatekey)
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_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print("transaction hash",transaction_hash)
return transaction_hash
max_retries = 3
retries = 0
transaction_hash = send_transaction()
while retries < max_retries:
try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
print("Transaction confirmed:")
break
except TimeExhausted:
retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
transaction_hash = send_transaction()
else:
print("Failed to confirm the transaction after multiple attempts.")
return TimeoutError
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed
tx_id = transaction_hash.hex() tx_id = transaction_hash.hex()

View File

@ -1,17 +1,14 @@
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" } ]'
# web3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"))
# mainnet # mainnet
RPC = 'https://polygon-rpc.com' RPC = 'https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e'
CONTRACT_ADDRESS = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c' CONTRACT_ADDRESS = '0x3a5d033CdF38aC4a9fe116CE88EBA2E93260044c'
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" }, { "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" } ]'
@ -23,15 +20,32 @@ 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:
print("private Key",privatekey)
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.UploadScriptAuditData(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.UploadScriptAuditData(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)
print("transaction hash",transaction_hash)
return transaction_hash
max_retries = 3
retries = 0
transaction_hash = send_transaction()
while retries < max_retries:
try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash)
print("Transaction confirmed:")
break
except TimeExhausted:
retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
transaction_hash = send_transaction()
else:
print("Failed to confirm the transaction after multiple attempts.")
return TimeoutError
trancation_fee = transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed trancation_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(trancation_fee)
@ -70,3 +84,4 @@ def deleteScriptAudit(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)})

View File

@ -1,32 +1,60 @@
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")) # 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" } ]'
# mainnet
# RPC = 'https://polygon-rpc.com'
RPC = "https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e"
CONTRACT_ADDRESS = '0xa3eE891d2b0bE5bcc99f14AFF37b42161Cae8E7B'
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))
# mainnet
# CAddress = '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" } ]'
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:
print("private Key",privatekey)
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.UploadScriptPadData(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.UploadScriptPadData(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)
print(web3.eth.gas_price)
print("transaction hash",transaction_hash)
return transaction_hash
# Define the number of retries
max_retries = 3
retries = 0
transaction_hash = send_transaction()
while retries < max_retries:
try:
transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash,timeout=60)
print("Transaction confirmed:")
break
except TimeExhausted:
retries += 1
print(f"Transaction not confirmed in {120 * retries} seconds, retrying... ({retries}/{max_retries})")
transaction_hash = send_transaction()
else:
print("Failed to confirm the transaction after multiple attempts.")
return TimeoutError
trancation_fee= transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed trancation_fee= transaction_receipt.effectiveGasPrice*transaction_receipt.gasUsed
tx_id = transaction_hash.hex() tx_id = transaction_hash.hex()
print("tx_id")
return tx_id,str(trancation_fee) return tx_id,str(trancation_fee)
except Exception as e: except Exception as e:
# print({"error":str(e)}) # print({"error":str(e)})
@ -63,6 +91,6 @@ 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',"fhdsjkdfd") # script = UploadScriptPadData("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",'0xd7F252B08B19e35344ac44DE7CCdd26D10Cc6e17',1,'1752aa50-a751-4149-84ad-680dcb932972',"pravesh11")
# print(script) # print(script)
# getScriptPad("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",1,'1752aa50-a751-4149-84ad-680dcb932972') # getScriptPad("6f06e1108b833b1918067042e13e60eda262705b80385a02d0330ce0db31d3ad",1,'1752aa50-a751-4149-84ad-680dcb932972')