So this question seems trivial but I can't find any comprehensive answer, though reading through:
- Is there a gas limit for view requests?
- Why view/pure functions need gas?
- Exceeds block gas limit error to call view functions
- Can Solidity constant functions be arbitrarily complex?
- Solidity Exceeds block gas limit during Mocha Tests with Ganache-cli
- Block Gas Limit Doubt
- Gas consumption of a view public function
- How to interpret the "Gas Limit" column on etherscan's internal txns view?
- High gas consumption when using `staticcall` on a non-view/pure function
My question is as follows:
- I can call
estimateGas
to estimate the gas needed to call one of my functions fromethers.js
- What is the limit gas for my function?
Context: I am working on a on-chain NFT project and the tokenURI
method is a bit complex. I don't understand if this is a problem. And how much gas maximum it should have?
Best Answer
estimateGas
will return a reasonably accurate value for yourtokenURI
method.(If your
tokenURI
method is very contrived/variable, be aware of What are the limitations to estimateGas and when would its estimate be considerably wrong?)Users generally do not invoke the
tokenURI
method so you are usually OK.The effect of any expensive smart contract function (including
tokenURI
) is less on-chain composability/interoperability with that function, possibly the whole contract.A web2 analogy might be calling a slow, unreliable, or paid API: developers will avoid calling those APIs in preference for something faster and reliable.
We can say that a very expensive
tokenURI
method would be one that consumes half the block gas limit. Even 10% of the block gas limit seems high, so I would aim for lower than 10%.Even though
tokenURI
is usually only called off-chain so that its gas usage doesn't matter as much, I would not underestimate the chances that there may be creative uses oftokenURI
on-chain in the future, so I would still try to be reasonable about its gas consumption.