首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web3.py事务总是超时

Web3.py事务总是超时
EN

Ethereum用户
提问于 2022-07-28 20:20:02
回答 1查看 195关注 0票数 1

我试图使用web3.py在我创建的地址(我在goerli上的元问询帐户)之间发送一个事务。在确认事务之前,一切似乎都进行得很顺利,此时w3.eth.wait_for_transaction_receipt()总是超时,并且在检查地址的余额时,事务永远不会通过。

下面是我的代码。

代码语言:javascript
复制
from web3 import Web3
#Import for ethereum account creation, does not require node connection
from eth_account import Account

#Necessary middleware to connect to goerli
from web3.middleware import geth_poa_middleware

#Necessary to sign transaction
from eth_account.messages import encode_defunct

#Intialize metamask address
metamask_address = "0xAfADBc095D871d58ee2D170CC52443C51169F06e"

#Infura goerli
w3 = Web3(Web3.HTTPProvider("https://goerli.infura.io/v3/{api key}"))

# inject the poa compatibility middleware to the innermost layer
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
print(w3.isConnected())

#Verify no accounts available
accounts = w3.eth.accounts
#Create account, after creating save key & address
"""
acct = Account.create('{entropy}')
acct_address = acct.address
print(acct.address, acct.key)
"""
acct_address = '0x74f04fBC4d7DA3b38CF94de15C2B6ee7cA0BFe62'
acct_key = b'{private key}'


#Get Balance after sending geth from metamask
acct_balance = w3.eth.get_balance(acct_address)
print(acct_balance)
print('w3 gas price', w3.eth.gas_price)

#Create transaction of 0.01 geth back to metamask account
transaction = {
  'from': acct_address,
  'to': metamask_address,
  'value': 10000000000000000,
  'gas': 100000,
  'gasPrice': w3.eth.gas_price,
  'nonce': 15
}

#Sign Transaction
signed_transaction = w3.eth.account.sign_transaction(transaction, private_key=acct_key)
#print('dir of signed_transaction', dir(signed_transaction))
#Send Transaction
transaction_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print('txn hash', transaction_hash)

transaction_receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)

#Check Balance again of both 
print(w3.eth.get_balance(acct_address))
print(w3.eth.get_balance(metamask_address))
#print('txn hash', transaction_hash)

我一直遇到的错误是

代码语言:javascript
复制
web3.exceptions.TimeExhausted: Transaction HexBytes('0x4466eb5190201f1f699867abaa31442da2258d05c8d23ca1f45b2af286b236d1') is not in the chain after 120 seconds
EN

回答 1

Ethereum用户

发布于 2022-07-29 13:18:06

这是我用于传输的代码,我刚刚测试了它,它在Goerli上工作。

我已经把你的地址放在“发件人”和“接受者”里了,只是丢失了你喜欢放的私钥!

代码语言:javascript
复制
from web3 import Web3

node_url = "GOERLI_ENDPOINT"  # Goerli endpoint URL
web3 = Web3(Web3.HTTPProvider(node_url))  # establish connection to a node

# verify the connection is successful (will return true)
if web3.isConnected():
    print("Connection Succsessful")
else:
    print("Connection Failed")

# set the adresses
# build transactions
# sign

sender = "0x74f04fBC4d7DA3b38CF94de15C2B6ee7cA0BFe62"
receiver = "0xAfADBc095D871d58ee2D170CC52443C51169F06e"
private_key = "YOUR_PRIVATE_KEY"

# nonce
nonce = web3.eth.getTransactionCount(sender)

# build the transaction
tx = {
    "nonce": nonce,
    "to": receiver,
    "value": web3.toWei(0.01, "ether"),
    "gas": 20000000,
    "gasPrice": web3.toWei(10, "gwei"),
    "chainId": web3.eth.chain_id,
}

# sign tx
signed_tx = web3.eth.account.signTransaction(tx, private_key)

# send transaction
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print("Transaction hash:", web3.toHex(tx_hash))

它也可能与您正在使用的端点有关。我使用钱斯塔克 (这是免费的),它工作得很好。

按照以下步骤在Chainstack上注册,部署节点,并找到端点凭据:

  1. 与Chainstack签约
  2. 部署节点
  3. 查看节点访问和凭据

他们在文档中也有一个包含大量调用示例的页面,而且代码实际上是工作的!

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/132588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档