I am developing a smart contract and I recently updated Hardhat to 2.6.x, which has london hardfork as default network settings. My issue is that gas used by my transaction increased by more than 50% in most cases. When I switch back to berlin hardfork, the cost is "correct" again.
Can somebody explain me why? Is there some updated opcode gas cost list? Or did gas cost for storing / loading values from storage increased? I am failing in finding the source of the increase.
I read EIP-1559, but there is no much information about opcode updates, just gas price and model of a base fee burning, which should not increase transaction gas usage imo.
I use ethers and property gasUsed on transaction receipt to get used gas.
It looks like EIP-3529 is the reason of the increase. EIP-3529 is reducing gas refunds and for one NFT transfers its ~20k gas increase because while transferring NFT you set storage of previous owner and (in some cases) his balance to 0, which are refunded less after london hardfork. Unfortunate.
Related
Gas costs for even a simple contract deployment are astronomical. If I spin up my own node and connect directly to it can I deploy on that node and avoid the gas fees?
Can I avoid gas fees by running my own Ethereum node?
Simple answer: No.
A node accepts the transaction, stores it in its mempool, and relays it to other nodes so that they can also store it in their mempool. There's nothing related to gas costs at this point.
Theoretically you could deploy a contract for "free" by running your own miner. But that's impossible without professional hardware costing very large amounts - many times more expensive than what you'd save on the gas fees.
However, in a block that you mined, you could include a transaction deploying a contract with 0 gas price. This sometimes happens - miners do occasionally put their own 0-priced transactions in their own blocks. But apart from that, no miner would accept your transaction with 0 gas price (or any transaction priced lower than the current market rate), as that wouldn't be profitable for them.
If I were to create my own Ethereum based crypto token named "ExampleCoin" with a supply of 1,000,000,000 and decided to distribute it to users for playing my game, would I need to pay a gas fee each time I give a user some ExampleCoin?
And if I had an in game store where users can make purchases with ExampleCoin, would a gas fee need to be paid in order to send ExampleCoin from the user to the game?
Gas is like gasoline to the Ethereum network; whenever we interact with the blockchain and affect its state, we must pay the fee required to perform the computations and process the transaction [1]. Each action such as storing values, sending tokens, adding integers etc. has associated cost in gas units [2]. The total gas cost for a transaction is multiplied by a gas price [3] to get the gas, or transaction, fee.
The transaction in which the ExampleCoin contract allocates some ExampleCoin tokens to a user, will be initiated by the user who will have to pay the required gas fee. Thus, it is the user who will initiate the transaction which will allocate the tokens to him, and thus he will pay the gas fee.
Similarly, when the user makes purchases with ExampleCoin, he is making a transaction affecting the state of the blockchain and thus incurring gas cost; so, he will have to pay the gas fee.
The bottom line is that whoever makes a transaction will have to pay the required gas fee. In this case, it would be the user, and not your contract, who pays the gas fee.
It is because of the increasing gas prices and thus transaction fees on the Ethereum network that solutions like sidechains, and other blockchains focusing on low transaction fees such as Solana have been developed. Thus, as a smart contract/ dapp developer, one has to comprehensively ascertain the business logic of one's application and decide which network to deploy it on, keeping the end user's experience in mind.
[1] See these explainers on gas and transactions:
https://ethereum.org/en/developers/docs/gas/ https://ethereum.org/en/developers/docs/transactions/
[2] See page 27 of the Yellow Paper for a list of operations and associated gas cost in gas units. For example, the base gas cost for every transaction is 21000 gas units: https://ethereum.github.io/yellowpaper/paper.pdf
[3] This is the amount in ETH we are willing to pay to the miner, per gas unit, as incentive to include our transaction in the next block. The higher the gas price, the more quickly our transaction is picked up by miners and mined.
If this is your private network and you do not want to use the gas mechanism to pay for transactions, then use Quorum - it does not require a transaction fee, and smart contracts are similar to Ethereum.
If you want to use Ethereum in your network, then you will have to come up with a mechanism for automatic distribution and compensation of Eth on user accounts. The simplest option is to give 100,000 Eth to the user's account when registering.
I deployed my smart contract(ERC-721) on truffle (Rinkeby | Ropsten | local node) and again on Remix and I keep getting an average cost of 0.0165 ether. This gas fee seems unrealistic to me, even though my contract is pretty simple.
I finally tried deploying to MAINNET using Truffle and the transactions stopped due to a low gas value. I switched over to REMIX and the new total gas fee is 0.65 ($2500) ether.
Is this a realistic amount to deploy a smart contract? or do I need to change some setting on remix?
Do the testnet's give a good representation of what gas fee will cost on mainnet ?
The amount of gas used from gas limit, not the gas price is going to be consistent between mainnet and testnets as the gas used from gas limit represents the amount of work that needs to be done to process the transaction logic in the EVM. Gas price (how much you pay for a unit of gas used) fluctuates as it is dependent on market economics/game theory, which is going to be very different on a test network vs live network.
References
https://ethereum.org/en/developers/docs/gas/
I'm working on sending transactions from one address to another on the goerli testnet and all transactions so far are not confirming. They have been staying in a pending state: https://goerli.etherscan.io/tx/0x056187763bac9adc8696fa0554c26b2f0e8ac48601dd4e5f03a30536d6597bf0
Did I do something wrong? I see the transaction in the etherscan.io/tx, but do I need some sort of callback handler?
Is this because there are not enough miners on the goerli testnet? Is Kovan the best testnet for ethereum dapp/smart contract development where transactions are being confirmed more often?
Any help is appreciated.
The linked transaction offers gas price of 0.000000002 Gwei (which is 2 wei). Usual current gas price on the Goerli network is 2 Gwei (1 billion times larger).
So it just seems that your sending script incorrectly calculates the decimals for the gas price.
Most miners order transactions by the gas price. So if there were more miners or less pending transactions, there would be a higher probability of this transaction making it to the block. But in the current situation, you need to raise the gas price of your transaction in order to compete with the other pending transactions.
If you want to replace the gas price on this particular transaction, you can send a new one with the same nonce but higher gas fee.
Is it possible to store gas that is not used inside a contract, so it can be used at a later time (presuming it is possible to require a certain amount of gas calling a function)?
I am trying to write a contract that requires user input and does something based upon that input at a later moment in time that will require gas.
As it does not sound really attractive to pay that gas out of the contract owners pocket I am looking for a way to make the user of the contract pay for the gas it needs to complete the request.
In fact, it is really possible to store gas in a contract for later use.
There are some operations in EVM that can return some gas that was payed before:
SSTORE: changing storage value from non-zero to zero releases 15000 gas
SELFDESTRUCT: destroying contract releases 24000 gas
That means that e.g. storing some value requires 20k gas, but deleting it form storage requires only 5k gas and releases 15k gas for later usage in the same transaction. Actually this is a reward for clearing up blockchain storage.
You can get more details by searching "refund" in Yellowpaper.
There is GasToken project that uses this very feature to store gas in contract when it is cheap and release (and use) when it is expensive.
To be clear, I don't think this allows user to issue transactions without paying gas at all.
Seems like you have mixed up a little bit the meaning of gas.
The gas is the Ether you have to pay to have your transaction mined. The gas is always paid by the address that calls the function(Contract) and not from the Contract itself or the owner of the Contract so it doesn't come out of the owners pocket. Also gas is basically ether so "storing gas" is to store Ether in a contract but you cannot store the gas that is used to mine the transaction. If you want to store Ether you have to send Ether to the Contract or have the users send Ether when they call a function.