Exceeded Prepaid Gas Common Solutions
Asked Answered
O

0

25

This post will outline the most common source of the famed "Exceeded the prepaid gas." error and how you can try and debug the problem. With that being said, I'm quickly going to outline some common misconceptions about GAS as well as some things you may not know.

  • GAS != attached deposit. By increasing the attached deposit, you are not attaching more GAS. For a more detailed explanation on GAS, see this document
  • The default GAS that will be attached if you do not specify a flag (--gas=) on the NEAR-CLI is 30 TGas.
  • The maximum GAS you can attach on the NEAR-CLI is 300 TGas. This can be done by appending the following flag: --gas=300000000000000

With that out of the way, let's dive into the most common source of the problem: cross-contract calls. In this scenario, you'll commonly see that you've attached more GAS than you've used as shown here:

enter image description here

This might be confusing as how can you be exceeding your prepaid GAS if your used GAS is less than your attach GAS? This is a very good indicator that you're dealing with cross-contract calls. The scenario is as follows:

We have a smart contract A with a function foo. This function will perform a cross-contract call to smart contract B and invoke the function bar while attaching 40 TGas.

  • Benji calls foo on contract A and attaches 10 TGas. We now have 10 TGas available.
  • The function executes logic (such as assertions etc..) before reaching the cross contract call. All of this logic so far has used 9 TGas. We now have 1 TGas available since we attached 10 and used 9.
  • The function tries to perform a cross-contract call and attaches 40 TGas. This will cause the prepaid gas error since you have 1 TGas available and you're trying to attach 40 TGas. This results in the explorer showing that 9 TGas was used but you attached 10 TGas.

TLDR: people attempt to attach GAS to a cross contract call (usually in the form of constants as shown here) but they don't have enough GAS available at the time.

There are two fixes for this. Either attach more gas to your initial call or decrease the amount of GAS you are attaching to your cross-contract call.

A more complex scenario people run into:

Let's say you're running into the exceeded prepaid GAS error on a function that was invoked as a result of a cross-contract call. Increasing your initial attached GAS will not do anything. Let's look at the following scenario to see why.

  • Benji calls foo on contract A and attaches 100 TGas. This will perform a cross contract call to contract B and invoke the bar function while attaching 40 TGas.
  • the bar function now has 40 TGas available. If you increase your initial attached GAS to 200 TGas, the bar function will still have 40 TGas since you'll still be attaching 40 TGas to the cross-contract call.

For this reason, the only way to increase the amount available on contract B is to increase the GAS being attached to the cross-contract call. You need to be careful when doing this, however, as if you were to increase it too much, you might run into the first scenario we looked at.

Odessaodetta answered 22/12, 2021 at 16:52 Comment(1)
this is so helpful @BenjiCoquette

© 2022 - 2024 — McMap. All rights reserved.