from web3 import Web3 import time # from .blockchainsetting import RPC_URL web3 = Web3(Web3.HTTPProvider("https://rpc-mumbai.maticvigil.com/")) # web3 = Web3(Web3.HTTPProvider(RPC_URL)) # web3 = Web3(Web3.HTTPProvider("https://polygon-mainnet.infura.io/v3/017957497a0d4e9c80750c18a431ac1e")) # 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": "UploadNarrationData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "deleteNarrationData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getNarrationData", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct NarrationDataBase.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" } ]' # CAddress = '0x2be58270E2F6067e08195DF2f7D466db71307282' CAddress = '0x71ec6aa6adbB56A14002750Dfe91e15380dfae8b' 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": "deleteNarrationData", "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": "UploadNarrationData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "user_id", "type": "uint256" }, { "internalType": "string", "name": "project", "type": "string" } ], "name": "getNarrationData", "outputs": [ { "components": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "string", "name": "mydata", "type": "string" }, { "internalType": "bool", "name": "Write", "type": "bool" } ], "internalType": "struct NarrationDataBase.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" } ]' contractInst = web3.eth.contract(address=CAddress, abi=abi) def NarrationData(privatekey,pubkey,user_id,project,data): try: acc1 = web3.eth.account.from_key(privatekey).address nonce = web3.eth.get_transaction_count(acc1) gas_price = web3.eth.gas_price uploadData = contractInst.functions.UploadNarrationData(pubkey,user_id,project,data).build_transaction({ 'gasPrice': web3.eth.gas_price, 'chainId': 80001, 'from': acc1, 'nonce': nonce}) signed_transaction = web3.eth.account.sign_transaction(uploadData, private_key=privatekey) transaction_hash = web3.eth.send_raw_transaction(signed_transaction.rawTransaction) transaction_receipt = web3.eth.wait_for_transaction_receipt(transaction_hash) gas_used = transaction_receipt['gasUsed'] trancation_fee = gas_used*gas_price tx_id = transaction_hash.hex() return tx_id,trancation_fee except Exception as e: print({"error":str(e)}) def getNarrationData(private_key,user_id,project): ACCOUNT = web3.eth.account.from_key(private_key).address try: getData = contractInst.functions.getNarrationData(user_id,project).call({'from':ACCOUNT}) 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