Gas cost is not the same as gas price.
The code in your smart contract equates to an equivalent set of EVM instructions. Each instruction has an associated gas cost, depending on how intensive an operation it is. Gas costs have been discussed in previous threads:
The gas cost of a given contract is intrinsic. You can't set it yourself, though it's recommended you try to code your contract in such a way to minimise the number of instructions, and therefore keep the associated gas cost to a minimum.
Gas costs are also referred to as gasUsed
.
Gas price (gasPrice
), on the other hand, is the amount that you are willing to pay per unit of instruction (i.e. per unit of gas cost). It's given in wei (1e-18 ether - see https://ethgasstation.info/).
Gas prices form a market. The number of transactions the network can support is limited by the size of the blocks, and other throughput mechanics. If the network is saturated, then someone is going to miss out. Setting a higher gas price allows you to price your transaction into the market, ahead of other lower-priced transactions.
So, overall:
Total cost = gasUsed
* gasPrice