我正在尝试在使用celery和kafka运行的任务中,在基于Django的Web应用程序中使用web3。但是,在尝试使用web3.eth模块时,出现以下错误:
/blockchain_transaction_service.py", line 43, in create_signed_transaction
nonce = w3.eth.getTransactionCount(from_address)
File "/home/paras/.local/lib/python3.6/site-packages/web3/eth.py", line 234, in getTransactionCount
return self.web3.manager.request_blocking(
AttributeError: 'HTTPProvider' object has no attribute 'manager'
起初,我收到一个错误,指出HTTPProvider没有属性eth,所以我遵循了这个answer的提示来解决此错误,但是我偶然发现了这个错误。奇怪的是,如果我不加芹菜运行它,它就可以正常工作。以下是我的代码段的外观:
from web3 import HTTPProvider, Web3, exceptions
from web3.eth import Eth
from .constants import *
from .enums import *
from .models import *
from .SendOTP import send_gaspod_alert_mail
import logging
import ast
import os
import requests
import json
logger = logging.getLogger(__name__)
#Initialize web3
w3 = Web3(HTTPProvider(INFURA_MAIN_NET_ETH_URL))
# Global for exception message in Ethereum transactions
OUT_OF_GAS_ERROR_CODE = -3200
OUT_OF_GAS_ERROR_CODE_ETH = -32000
OUT_OF_GAS_MESSAGE = "insufficient funds for gas * price + value"
def create_signed_transaction(to_address,from_address,amount,private_key,gas) :
data = {}
try :
if to_address is None or from_address is None or gas is None or private_key is None or amount is None:
raise Exception("Invalid params")
if amount <=0 :
raise Exception("Invalid parameter amount cannot be less or equals to 0")
to_address = Web3.toChecksumAddress(to_address)
from_address = Web3.toChecksumAddress(from_address)
w3 = Web3.HTTPProvider(RPOSTON_INFURA_TEST_URL)
Eth.attach(w3,"eth")
print(w3)
print(type(w3))
nonce = w3.eth.getTransactionCount(from_address)
gas_price = w3.eth.gasPrice
value= Web3.toWei(amount,"ether")
signed_txn = w3.eth.account.signTransaction(dict
(
nonce=nonce,
gasPrice = gas_price,
gas = gas,
to=to_address,
value=value
),
private_key)
data["signed_txn"] = signed_txn
return data
except Exception as e :
import traceback
print(traceback.format_exc())
data = {}
error = "Unable to create signed transaction " + str(e)
data["signed_txn"] = None
data["error"] = error
return data
有人可以告诉我,我在哪里错了?