I am attempting to deploy a simple erc 20 token on the ethereum mainnet. I have tried twice now to deploy the contract each time paying around $20 in gas fees for contracts that would run out of gas before being deployed. I customized the fees (mistake on my part since this is my first time) and set the gas limit to 200000 and the gas price to 55 GWEI and the second time setting the gas limit to 400000 and the gas price to 20 GWEI. I have already looked at websites such as eth gas station. The recomended fees are around $80 which seems a bit extreme and I am wondering how can I lower gas prices? What should I set the gas limit to to ensure it deploys? What should I set the gas price to to ensure it deploys?
How much gas is required to execute a function depends on how complex that function's logic is. Each individual operation has a gas cost based roughly on how much computing resources will be required to perform that operation (e.g. writing to storage is much more expensive than adding two integers). The total gas cost of your function is the sum of the gas costs of all its individual operations.
The creators of Ethereum wanted to make sure someone couldn't clog up the network with an infinite loop, or hog all the network resources with really intensive computations. So they made it so transactions aren't free, and users have to pay for computation time as well as storage.