Setting Base Gas Fees of Your Precompile
Learn how to set the base gas fees of your precompile.
Gas is used for spam prevention. If our precompile is accessible to everyone on our Avalanche L1, it is important to set the gas cost in a way that lets users economically utilize it in their apps, yet prevents spammers from spamming our blockchain with calls to the precompile.
In this section, we'll modify the default values of the gas costs of our Calculator precompile. By default, all user-defined functions have a gas cost of 1. While this is good news for the average user, allowing users to call computationally expensive functions can leave our blockchain vulnerable to Denial-of-Service (DoS) attacks.
We can find the default gas cost values for our functions in calculator/contract.go. Following the import statements, you should see:
The variables AddGasCost
, NextTwoGasCost
, and RepeatGasCost
are the gas costs of the add
, nextTwo
, and repeat
functions, respectively. As you can see, they are currently set to 1.
However, changing the gas cost is as easy as changing the values themselves. As an example, change the gas costs to 7.