How to Debug Intermittent Contract Revert (Panic) in Smart Contract Transaction?

I have a smart contract with a simple purpose. It has a function that takes an id and an address as input, performs checks based on the id, and then transfers funds to the specified address.

However, for one of the transactions, it reverted with a panic. Sometimes it executes successfully, while other times it throws a panic and reverts.

How can I determine the exact reason for the contract’s panic ?

Here is the TypeScript code used to send the transaction:

contract.functions
                .watch(sequenceNumber, receiver)
                .txParams({
                    variableOutputs: 1,
                })
                .callParams({
                    forward: [parsedAmount, assetId.bits],
                })

Here is the Panic Receipt

{

pc:"10384",

is:"10368",

reason:"589198682725810176",

receiptType:"PANIC"

}

Any insights on debugging this would be appreciated!

1 Like

@nerses Does the error message simply say reverted with a panic?

The TS SDK parses the receipts and error messages returned by fuel-core before throwing the error.

It is usually something like this:

The transaction reverted with reason: OutOfGas

Before throwing an error due to a transaction submission error, the TS SDK adds some data to the JS Error object which can be captured by adding a try/catch block to the transaction execution code. Like:

try {
  const call = await contract.functions
      .watch(sequenceNumber, receiver)
      .txParams({
          variableOutputs: 1,
      })
      .callParams({
          forward: [parsedAmount, assetId.bits],
      })
      .call()
  
  await call.waitForResult()
} catch (error) {
  console.log(error)
}

The result object is something like:

{
  VERSIONS: { FUEL_CORE: '0.40.4', FORC: '0.66.6', FUELS: '0.98.0' },
  metadata: {
    logs: [...],
    receipts: [...],
    panic: true,
    revert: true,
    reason: 'OutOfGas'
  },
  rawError: null,
  code: 'script-reverted'
}

How can I determine the exact reason for the contract’s panic ?

Unfortunately, there is no way to extract a readable panic reason from the panic receipt reason property value.

The TS SDK extracts the reason from the error message thrown by fuel-core instead.

This is the list of panic reasons: PanicReason in fuel_asm - Rust

3 Likes