I’ like to share a short learning experiance from “beginners learning”.
Topic : Restart does not reset the ganache network
A Problem which is easy to understand, but at beginners learning drive you nuts. Something that feels like a ganache problem
- You get error messages from variables or function, that does not exist anymore in your contract
- You change something in deployment or smart contract. And then nothing changes
- You get strange error messages that does not relate to what you changed
- You change something in you deployment script and things running more and more strange
- Occurs if you run ganache or ganache-cli for a very long time, e.g. multiple suspends of the working environment or virtual instance
- And your code/code snipped seems to be absolute ok
So this seems to be often an environment problem when working with ganache and truffle
I was not the first ran into this… from github project
Ah! I finally figured out what this is all about. This isn’t a bug with Ganache or even really Truffle, but this is why this is being observed:
Get a fresh Truffle project with your contracts
Compile the contracts, this makes your build/contracts/contract.json artifact files
When you do truffle migrate, it checks the artifact file and sees that it hasn’t been deployed to the network your interested in (say your development network in truffle-config.js), so it deploys a new contract
Truffle takes the address of the new deployed instance and writes it in your artifact file
You want to change some stuff, you reset Ganache
Ganache does in fact start a new chain, so the previously deployed version of your contract is now wiped
You run truffle migrate again, but Truffle sees your artifact file has an existing address, so Truffle attempts to do a migration rather than just a deployment
Truffle does like a getCode (or similar check) on this address of a previously deployed instance, and sees there is no deployed contract there, and Truffle errors saying that it’s already deployed, but can’t find the contract at the address or something along those lines
This behavior is by design with Truffle Migrations. To prevent Truffle from looking at the existing address, you can specify the --reset option to do a new deployment. You can read more in the migration docs
If you believe there should be some sort of smarter logic to check whether or not the contract should get deployed if the existing address can’t be found, please feel free to open an issue on Truffle’s repo
Thanks for reporting this issue! Let me know if you have any questions!
It’s not a ganache problem cause this behavior. It was the migration process that got tired.
Delete all jour json files in build/contracts/* and recompile
Well even it drive me almost crazy, but sometime you just search on the wrong side Writing smart contracs needs a broughter view when getting into troubles!