Deploying Expensive Contracts – Why Contract Deployment is Costly and How to Reduce Gas Fees

contract-deploymentfeesgasgolangout-of-gas

I've tried to deploy a contract based on openzeppelin library (see below). The fee I paid is about $146 and the contract didn't even deploy(i.e it run out of gas ). I find this unbelievable high. It really cut my enthusiasm about Ethereum as I regarded it more like a platform than a pozi/value-store (i.e bitcoin).

ethscan.io says the contract failed as it run out of gas.

 Warning! Error encountered during contract execution [Out of gas]
Value:
0 Ether ($0.00)
Transaction Fee:
0.034104011574 Ether ($146.01)
Gas Price:
0.00000011368003858 Ether (113.68003858 Gwei)
Gas Limit & Usage by Txn:
300,000 | 300,000 (100%)
Gas Fees:
Base: 104.963834926 Gwei

Did I do something foolish and the miners "stole" my fees or is this the market price?

Is there any way to estimate the cost of the contract creation/initialization ( I guess whatever it has in the init function) using Go ? I deploy it with Go anyway using the bindings generated by ethereum dev tools ( "github.com/ethereum/go-ethereum/accounts/abi/bind")

Source code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";

contract XYZXTESTToken is ERC20 {
        uint256 private constant TOTAL_SUPPLY = (18000 * 10000000 * (10**18));
    constructor() ERC20("XYZXTESTTOKEN", "XYZXTESTTOKEN") {
        _mint(msg.sender, TOTAL_SUPPLY);
    }
}

Best Answer

Did I do something foolish and the miners "stole" my fees or is this the market price?

You didn't do anything foolish except setting a relatively low gas limit. The miners didn't steal your fee, you just used the wrong parameters, the tx can succeed or fail, but gas will have to be paid no matter what.

I deployed your contract using Remix and the tx gas cost is : 1,154,936

Which means, 3.8 times more that the limit of 300,000 that you used.

Now I'd like to get back a bit on what I said, you did something kind of foolish: not making sure that you'd get what you were paying for. This means making sure that your parameters (i.e., gas price / gas limit) have values allowing the deployment of your smart contract.

You should only deploy to mainnet when you are sure that everything is working, the mainnet is NOT a test sandbox.

If you didn't do it already, I would advise you to use local blockchains and / or tesnets such as Rinkeby or Ropsten. Alternatively, you can also deploy to L2 solutions such as Polygon where gas price is more affordable.

There are several development framework simplifying dev / test and deployment such as Truffle, Hardhat or Brownie to cite the most accessible ones but it's either JavaScript or Python.

However, if you absolutely want to stick with go, and go-ethereum bindings you could be interested by simulated clients or setting up a local blockchain with go-ethereum.