I kind of accidentally deployed an ERC20 token contract on the mainnet and brownie didn't even ask me to confirm so I ended up spending $700+ worth of ETH to practice. I didn't think it would cost that much and for some reason brownie didn't ask me for permission like metamask usually does to confirm that I'm ok with the transaction fees. How can I avoid this or atleast confirm that I'm ok with transaction fees with eth-brownie? Should I programmatically check if I'm ok with the transaction fee associated with a given transaction, if so best way to go about that? Should I be focused on gas limit? Should I set a gas limit on each transaction? Also how do I see the estimated gas for a transaction with eth-brownie?
2 Answers
Got on track by this blog post :
from brownie import accounts, web3, MyContract
account = accounts.load("my_account")
contract = MyContract.deploy({"from": account})
# contract = MyContract[len(MyContract)-1] # if contract is already deployed
mocked = web3.eth.contract(
address = contract.address,
abi = contract.abi
)
estimated_gas = mocked.functions \
.functionToBeTested(function_payload) \
.estimateGas({"from": account.address})
print(estimated_gas)
BTW, you can retrieve the current gas price with
print(web3.eth.gasPrice)
and the latest block average gas limit with
last_block = web3.eth.getBlock("latest")
gas_limit = last_block.gasLimit / (len(last_block.transactions) if last_block.transactions else 1)
print(gas_limit)
-
For search's sake, in my case I encountered
execution reverted: ERC721: mint to the zero address
error because I missed theestimateGas()
arguments. Jan 28, 2022 at 17:52
Skippys' answer is the correct one, i will put another implementation here in case anyone wants to know: P.S: here is the complete walk through link
from web3 import Web3, HTTPProvider
import statistics
web3 = Web3(HTTPProvider("ADD_YOUR_ETHEREUM_NODE_URL"))
pending_transactions = web3.provider.make_request("parity_pendingTransactions", [])
gas_prices = []
gases = []
for tx in pending_transactions["result"[:10]]:
gas_prices.append(int((tx["gasPrice"]),16))
gases.append(int((tx["gas"]),16))
print("Average:")
print("gasPrice: ", statistics.mean(gas_prices))
print(" ")
print("Median:")
print("gasPrice: ", statistics.median(gas_prices))