After running Truffle tests, the blockchain reverts to a clean room environment and all the data is lost.

How can you do transactions from Truffle and persist the data in your contract? In my case I want to use that data in a web3 frontend.

From the frontend, the transactions are saved, but it is tedious as you must approve the MetaMask confirmation each time, and creating hundreds of transactions would be a pain, whereas in Truffle you can just loop and not worry about the confirmation messages.

1 Answer 1


As a point of clarity, what you are attempting to persist is the data on your private Ganache network (not Truffle). Truffle is simply the framework that allows you to create a project and point to different networks, such as the main net, Ropsten, Ganache, etc.

What you are trying to do cannot be accomplished with tests, but rather should be done on the deployment side (in the migrations folder of your Truffle project). The whole point of the test is to temporarily examine a piece of the code to confirm it works, not persist.

In order to achieve what you want done, you should perform the deployment you are attempting to perform in your migrations/2_deploy_contract.js file. When your code is in there, simply run truffle migrate, and your code will deploy on whichever network you are pointing to.

If you are trying to perform hundreds of transactions, you must either do it manually, or write a script (similar to what you are doing in your tests) that will interact with the blockchain network that you are on and perform the transactions. How you write it and what tools you use are up to you (web3js, web3.py, etc.), but they will all lead to the same outcome.

Take a look at this article for advice on how to use web3js to perform what you are attempting to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.