Published on

Debugging a Solidity Ganache Migration Issue with Truffle and Ganache

Authors

In Solidity I have often come across the following error when trying to run a truffle migrate against Ganache locally:

Error: Returned values aren't valid, did it run Out of Gas?

Not much information is given and the stack trace provided is unrelated to your code.

The steps I used to debug and fix this are below.

Does the deploying account have sufficient gas?

The obvious first check is does the account deploying the contract have sufficient gas?

With Truffle and Ganache this is the first account in the list of accounts you see when Ganache starts.

But to confirm for sure run the migrate using the following commands:

truffle migrate --verbose-rpc

The result will be a wall of text that has something like the following in it:

...
Deploying 'TheNameOfYourContract'
----------------------
   > {
   >   "jsonrpc": "2.0",
   >   "id": 52,
   >   "method": "eth_sendTransaction",
   >   "params": [
   >     {
   >       "from": "0x8396513daf2b8c279b18cefd56609b33d7f5cdcf",
   >       "gas": "0x6691b7",
   >       "gasPrice": "0x4a817c800",
   >       "to": "0x409e3654d432402f50d125c0b5eeac9ccd27647d",
   >       "data": "0xfdacd5760000000000000000000000000000000000000000000000000000000000000002"
   >     }
   >   ]
   > }
 <   {
 <     "id": 52,
 <     "jsonrpc": "2.0",
 <     "result": "0x0000000000000000000000000000000000000000000000000000000000000000"
 <   }
...

When debugging look at the end of the text and go upwards until you reach a chunk of text that looks similar to the above as this will have the details related to the error message.

There are a few things to look out for in this output:

  • Deploying 'TheNameOfYourContract': This is the name of the contract being deployed.
  • "from": This is the account that is deploying the smart contract in this case 0x8396513daf2b8c279b18cefd56609b33d7f5cdcf
  • "result": In the above result has an address of 0 which is what the original error message refers to.
    • An example of a successful result is 0xe49f1

The first thing to do is confirm that the from account0x8396513daf2b8c279b18cefd56609b33d7f5cdcf (in this case) actually has sufficient funds to deploy the contract.

We can check an accounts balance with the help of the truffle console as follows:

  • Run the truffle console:
truffle console
  • Type the following:
> truffle(development)> web3.eth.getBalance('0x8396513DAF2b8c279B18cefD56609B33D7F5cdcF')
'99960190140000000000'

In the above the balance is not 0 and is more than high enough to deploy a contract - the contract's balance in this case is not the issue.

Did the contract actually deploy despite the error?

Although there was an error message when we initially deployed it was quite cryptic so lets confirm for sure whether or not our contract actually deployed to Ganache. We can do this as follows:

  • Log into the truffle console
truffle console
  • Check if your contract is in the environment by pushing tab twice. You should see something similar to the below:
Array                 Boolean               Date                  Error                 EvalError             Function              Infinity              JSON                  Math
...
CallPerson            DataView              Float32Array          Float64Array          GLOBAL                Int16Array
...
Person                PersonI               Promise
...
toLocaleString        toString              valueOf
  • If your contract deployed successfully you should see it in the list

  • If it is in the list check if it deployed correctly i.e. it has an address for example for a contract called Person:

truffle(development)> Person.address
'0x70C9F52ecC3148fA4FE0a41CcCa635F0C64526C2'

In the above there is an address so the Person contract did deploy successfully.

Conclusion

After trying these steps I eventually came across the issue. In my case I found that:

  • The deploying address had sufficient funds.
  • My contract had deployed as I could see it in the truffle console tab list.

The issue came when I tried to get the contract's address in the truffle console e.g. Person.address - instead of the address a JavaScript error and stacktrace were returned.

It turns out my issue was caused by a corrupted build directory as I had copy pasted all folders in a directory from another dApp project to get up and running faster. As soon as I deleted the build directory and ran ganache-cli and truffle migrate again my contracts migrated without issues.