1

This question already has an answer here:

I'm using truffle and ganash to host local dev blockchains and neither seem to persist my contracts or state.

This is super painful as it means the contract address changes for my dApp each time i restart my environment and metamask's nonce gets out of sync all the time so i have to reset my account.

How can I resolve this please? Is it me who is wrong in my approach? 😂

marked as duplicate by Ismael, shane, Nicolas Massart, Richard Horrocks, Mahesh Rajput Jun 12 at 4:58

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

This did the trick for me

ganache-cli -d --db path/of/local/folder/here -i 123456
  • -d because we want to tell ganache to keep mnemonic deterministic. And not generate new accounts and new mnemonic every time ganache is restarted
  • -db so that the state of blockchain gets stored in a local folder on your machine. When ganache-cli is restarted if you use the same command which thereby reads from the local folder, your blockchain state is persisted and read without loss
  • -i so that a random networkID is not selected everytime you restart ganache

Make sure that the folder already exists where you intend ganache to store its data

1

Using ganache-cli

ganache-cli -d --db <DATA_DIR> -i <NETWORK_ID> --accounts 20 --deterministic --mnemonic="myth like bonus scare over problem client lizard pioneer submit female collect"

Options used

-d, --deterministic

Generate deterministic addresses based on a pre-defined mnemonic. [boolean]

this will generate the same addresses anytime.

-m, --mnemonic

bip39 mnemonic phrase for generating a PRNG seed, which is in turn used for hierarchical deterministic (HD) account generation [string]

This is mandatory while using --deterministic option, you have to supply the same mnemonic each time you start.

--db

Directory of chain database; creates one if it doesn't exist [string] [default: null]

You can add parameter db, to save/persist the chain data (with all the transactions) so you can load same contracts state again in the next executions

-i, --networkId

The Network ID ganache-cli will use to identify itself. [number] [default: System time at process start or Network ID of forked blockchain if configured.]

this make sure that random networkID is not selected everytime you restart ganache

And, make sure that the folder already exists where you intend to store/persist your local network data

Freebie

If you want to avoid leaving console open to run ganache, you can run as a background process by adding an ampersand(&) at the end of the command and write its output to log instead of your terminal. If you want to look for accounts, you can open a file and use tail -f file.log to watch for transaction log details.

so I'd recommend redirecting its output to a file like below:

<cmd> 2>&1 > ganache-output-$(date +%Y-%m-%d-%H:%M:%S).log &

If you want to find if the process is running, use ps -ef | grep ganache

# ps -ef | grep ganache
root       323     1  0 02:54 pts/2    00:00:03 node /usr/bin/ganache-cli -d --db /app/ganache_data -i 344 --accounts 20 --deterministic --mnemonic=myth like bonus scare over problem client lizard pioneer submit female collect
root       635 32727  0 03:49 pts/2    00:00:00 grep --color=auto ganache

where 323 is what's known as the "pid".

Finally, when you're done, you can either use kill <PID>(In this case kill 323) or you can just fg 1 and hit ctrl-c.

One last tip, I'd recommend creating a bash file say start_ganache.sh, so that you don't need to remember options and their values whenever you want to start/run.

start_ganache.sh

#!/bin/bash
ganache-cli -d --db /app/ganache_data -i 344 --accounts 20 --deterministic --mnemonic="myth like bonus scare over problem client lizard pioneer submit female collect" 2>&1 > /app/ganache/logs/ganache-output-$(date +%Y-%m-%d-%H:%M:%S).log &

give executable permission using chmod +x start_ganache.sh and execute ./start_ganache.sh

P.S: I know this is more verbose. But believe me, many junior developers tend to have a much smaller zone of focus when working with bash. I hope someone 'll find this useful

  • I don't think you have to specify both mnemonic and deterministic at the same time. Correct me if I am wrong. – Igor Yalovoy Apr 19 at 14:12
0

Are you using ganache-cli? It has the following parameter (from their readme):

--db: Specify a path to a directory to save the chain database. If a database already exists, ganache-cli will initialize that chain instead of creating a new one.

Looks like providing this parameter will do what you expect - persist the chain state between restarts

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