[英]Adding nonce to run multiple contract transactions with web3 python
我正在嘗試在 Python 中創建一個 ERC-1155 鑄幣機,它可以鑄幣數百(或更多)NFT,所以我不想等待每個成功。 如何將 nonce 添加到 contract_instance transact() function? 我似乎在 web3 py 文檔中找不到這個,或者有更好的方法嗎? 我也想知道我是否應該期望一些交易失敗並且可能有一些驗證步驟。 我確實嘗試在每個之間使用“wait_for_transaction_receipt”,但是每個單獨的鑄幣廠大約需要 10~ 秒,這可能太慢了,而且它似乎在大約 20-30 鑄幣廠后隨機失敗。
使用我當前的代碼,我在大約 20 多分鍾后收到此錯誤: ValueError: {'code': -32000, 'message': 'replacement transaction underpriced'}
感謝您提供的任何幫助!
這是我的代碼:
from web3 import Web3
from decouple import config
from eth_account import Account
from eth_account.signers.local import LocalAccount
from web3.auto import w3
from web3.middleware import geth_poa_middleware,construct_sign_and_send_raw_middleware
infura_url = config('INFURA_URL')
print(infura_url)
private_key=Private_Key
account: LocalAccount = Account.from_key(private_key)
w3 = Web3(Web3.HTTPProvider(infura_url))
w3.middleware_onion.add(construct_sign_and_send_raw_middleware(account))
print(f"Your hot wallet address is {account.address}")
w3.eth.defaultAccount = 'MYADDRESS'
#added for testnet
w3.middleware_onion.inject(geth_poa_middleware, layer=0)
res = w3.isConnected()
for x in range(200):
address = 'CONTRACT_ADDRESS'
abi = 'ABI_HERE'
contract_instance = w3.eth.contract(address=address, abi=abi)
thisvar = contract_instance.functions.symbol().call()
print(thisvar)
print(x)
#generate a random address for simulating a large list of addresses
mint2acc = Account.create('KEYSMASH FJAFJKLDSKF7JKFDJ 1530')
print(mint2acc.address)
thistx = contract_instance.functions.mint(mint2acc.address,1,1).transact()
在交易中,您可以像這樣放置參數
nonce = w3.eth.get_transaction_count(mint2acc.address)
contract_instance.functions.mint(mint2acc.address,1,1).transact({"nonce":nonce})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.