The gas limit for a view function call *not made from a contract*

gasgas-limitout-of-gassolidity

So this question seems trivial but I can't find any comprehensive answer, though reading through:

My question is as follows:

  • I can call estimateGas to estimate the gas needed to call one of my functions from ethers.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

What is the limit gas for my function?

estimateGas will return a reasonably accurate value for your tokenURI 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?)


tokenURI method is a bit complex. I don't understand if this is a problem.

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.


And how much gas maximum it should have?

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 tokenURIis 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 of tokenURI on-chain in the future, so I would still try to be reasonable about its gas consumption.

Related Topic