title: Exceeded Prepaid Gas Common Solutions
body: <p>This post will outline the most common source of the famed <code>&quot;Exceeded the prepaid gas.&quot;</code> 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.</p>
  <ul>
  <li>GAS != attached deposit. By increasing the attached deposit, you are <strong>not</strong> attaching more GAS. For a more detailed explanation on GAS, see <a href="https://docs.near.org/docs/concepts/gas" rel="noreferrer">this document</a></li>
  <li>The default GAS that will be attached if you do not specify a flag (<code>--gas=</code>) on the NEAR-CLI is 30 TGas.</li>
  <li>The maximum GAS you can attach on the NEAR-CLI is 300 TGas. This can be done by appending the following flag: <code>--gas=300000000000000</code></li>
  </ul>
  <p>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:</p>
  <p><a href="https://i.stack.imgur.com/5EPIn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/5EPIn.png" alt="enter image description here" /></a></p>
  <p>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 <strong>very good indicator</strong> that you're dealing with <strong>cross-contract calls</strong>. The scenario is as follows:</p>
  <p>We have a smart contract <code>A</code> with a function <code>foo</code>. This function will perform a cross-contract call to smart contract <code>B</code> and invoke the function <code>bar</code> while <strong>attaching 40 TGas</strong>.</p>
  <ul>
  <li>Benji calls <code>foo</code> on contract <code>A</code> and attaches 10 TGas. We now have 10 TGas available.</li>
  <li>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.</li>
  <li>The function tries to perform a cross-contract call and attaches 40 TGas. This will cause the <strong>prepaid gas error</strong> 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.</li>
  </ul>
  <p>TLDR: people attempt to attach GAS to a cross contract call (usually in the form of constants as shown <a href="https://github.com/near-examples/nft-tutorial/blob/e8f85847f93c73ac56f926315a3b6abc7290d7d1/nft-contract/src/nft_core.rs#L5" rel="noreferrer">here</a>) but they don't have enough GAS available at the time.</p>
  <p>There are two fixes for this. Either attach more gas to your <strong>initial call</strong> or <strong>decrease</strong> the amount of GAS you are <strong>attaching to your cross-contract call</strong>.</p>
  <p><em>A more complex scenario people run into:</em></p>
  <p>Let's say you're running into the exceeded prepaid GAS error on a function that was invoked as <strong>a result of a cross-contract call</strong>. Increasing your <strong>initial</strong> attached GAS will <strong>not do anything</strong>. Let's look at the following scenario to see why.</p>
  <ul>
  <li>Benji calls <code>foo</code> on contract <code>A</code> and attaches 100 TGas. This will perform a cross contract call to contract <code>B</code> and invoke the <code>bar</code> function while attaching 40 TGas.</li>
  <li>the <code>bar</code> function now has 40 TGas available. If you increase your initial attached GAS to 200 TGas, the <code>bar</code> function will <strong>still have 40 TGas</strong> since you'll still be attaching 40 TGas to the cross-contract call.</li>
  </ul>
  <p>For this reason, the only way to increase the amount available on contract <code>B</code> is to <strong>increase</strong> the GAS being <strong>attached to the cross-contract call</strong>. 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.</p>