Dapp development weapon-Ganache of Truffle Three Musketeers

一、Truffle Suite

       Truffle Suite provides tools and services for the rapid development of applications (Dapp) on Ethereum. It mainly has the following three products: truffle, ganache and drizzle. These three tools are very helpful for the rapid development of Ethereum DAPP, so I will call its three products "Truffle Three Musketeers" for the time being.
Insert picture description here
       The three products from left to right in the picture are truffle, ganache and drizzle. Today we will introduce Ganache first.

Second, what is Ganache

       What is Ganache? It is a one-click blockchain. The following is the original official document:
       A personal blockchain for Ethereum development you can use to deploy contracts, develop your applications, and run tests. It is available as both a desktop application as well as a command-line tool (formerly known as the TestRPC) . Ganache is available for Windows, Mac, and Linux.

       The general meaning is: it is a personal development environment of Ethereum, on which you can deploy contracts, develop programs, and perform tests. It has a desktop version and a command-line tool version, while providing support for windows, Mac and Linux.

       It can be seen that it is a local version of Ethereum. Why do we need to use the local environment when developing Dapp? The answer is speed of response. The development of Dapps on Ethereum is generally carried out on the testnet (because it is free). Although the response speed of the testnet is faster than that of the mainnet, it is sometimes congested. Basically, you need to wait more than ten seconds for a transaction, and the time of 10 seconds is still a bit long. And there is a hidden premise here, that is, you can connect to the test network, you can't live without the network, and sometimes you even need it infura_key.

       There is no such limitation when using the local development environment, and you can feel the feeling of instantaneous completion of blockchain transactions. Not only saves us a lot of development time, but also makes our development rhythm more consistent.

Three, Ganache settings

       Download link: https://www.trufflesuite.com/ganache . Download the version of the corresponding system, and then install it, this will skip silently. We focus on the settings of truffle.

       Open Ganache, as shown below:
Insert picture description here
       The button on the left is for quick start, its data will not be saved, and it is a brand new development environment after each start. The button on the right is to save the current data to the corresponding workspace. There can be multiple workspaces. We choose new workspace on the right:
Insert picture description here
       here you can take a meaningful name for the workspace, and skip the add project in the lower left corner because we don’t have a truffle project yet.

       Click on the server in the navigation bar, you can see the RPC service related settings:
Insert picture description here
       let it all be the default here, that is, the port number is 8545, and the network chainId is 5777. Then click Accounts & Keys on the navigation, here is to set the initial number of accounts and the initial amount of the account, we set the initial amount to 10000ETH.
Insert picture description here
       Then click chain on the navigation bar to enter the blockchain settings. Mainly look at the upper limit of the block gas, the default is 6721975. At the time of writing this article, the gas upper limit of the mainnet, kovan testnet and rinkeby testnet is already 10,000,000; the gas upper limit of the ropsten testnet is 8,000,000. So we changed it to 8,000,000 to be compatible with all networks, of course you can also use the default value.
Insert picture description here
       Ok, let’s skip setting other settings first, click the Save Workspace button in the upper right corner, and our local Ethereum will be available.

Four, Ganache main interface introduction

       After saving the workspace, the following screen will appear:
Insert picture description here
       The top column of this interface is the navigation menu, the next line is various information, and the right is the switch workspace and setting buttons. The third line is the mnemonic phrase, all addresses are generated based on it.

       The main body of the interface has 10 accounts, which respectively list the address, balance, number of transactions completed, index in the account array, and display private key button for each account. Click that key icon, and its private key will be displayed.

       Click Blocks on the navigation, you can see that the current Block is 0. This is because of the Ganache mining mechanism. Each transaction generates a block, so a block will only have one transaction (this is the author's experience, which may be incorrect). A block will be generated after our transaction, there is no one yet.

Insert picture description here
       Click Transactions, it will show no transactions. Click CONTRACTS, because the truffle project is not added to our workspace, so here is also empty.

       It should be noted that this event button will display no event after clicking it, because we have not triggered an event yet. But in the process of actual use, the event will not be displayed here (the reason remains to be studied).

       The last log will show some logs of Ganache, which we basically don't use.

Five, MetaMask connects Ganache

       You can connect MetaMask with our Ganache, so that the code can directly call MetaMask for transactions during the Dapp development process. Click the network selection button in MateMask and select localhost:8545.
Insert picture description here
       Remember when we set up Ganache before, the port number was the default value of 8545? If not, either change the settings in MetaMask or change the settings of Ganache to make the two consistent. After connecting to the local Ethereum, the screen is as follows: As
Insert picture description here
       you can see, the balance of my work account in the local Ethereum is 0. Let's add the account in Ganache.

       Go back to the Accounts interface of Ganache, click the private key of the first account, and copy it.
Insert picture description here
       In MetaMask, click on the upper right corner, select Import Account in the pop-up menu to
Insert picture description here
       paste the private key you just copied, click Import:
Insert picture description here
       Then you will enter the main interface of the wallet, the balance of the account has been displayed, it is the 10000ETH of the Ganache account:
Insert picture description here
       we can use MetaMask In the account details, change the name of the account to make it easier to distinguish:
Insert picture description here
Insert picture description here

Six, test Ganache

       We use Ganache account 1 to transfer 100 ETH to my work account through MetaMask for a simple test.

       Switch to my work account in MetaMask and copy the address. Then switch back to Ganache account 1, click the send button, paste the address of the work account in the recipient text box, and select 100 ETH as the quantity:
Insert picture description here
       click Next:
Insert picture description here
       check the information is correct, click confirm, and the transaction is completed instantly.
Insert picture description here
       Why did we only send 100 ETH, but the amount became a decimal? Because when we send ETH, we also consume a little fee.

       Switch to my work account and check if the balance is 100ETH: As
Insert picture description here
       you can see, we did receive 100ETH.

       Let's go back to Ganache and look at the changes in related content:
Insert picture description here
       here we show that our first account has made a transaction, and the balance is currently 9900.00ETH (the result after rounding).

       When we click on the transaction in the navigation bar, we will see this transaction. Click on it to expand the details: we
Insert picture description here
       can see some information about this transaction, including gas price, gasLimit and gasUsed, the amount of ETH in the transaction, and the packaged block.

We send ETH several times, including sending ETH to other accounts in the Ganache account list. At this time, there will be multiple records in the block list.
Insert picture description here
       Click on the record, it will expand to get a detail. This concludes our simple test.

Seven, summary

       Ganache, as it introduced, is very simple to use, one-click blockchain. Using it can not only greatly save development time, but also does not need to be connected to an Ethereum node on the Internet (either the main network or the test network needs to connect to a node). Of course, you don't need infura_keyto connect to the infura node.

       But Ganache is not a real Ethereum. There are still some differences between them (for example, the mining mechanism, which will cause repeated events to be received), and sometimes nonce will go wrong. However, these effects are not big, and the flaws are not concealed. I still recommend it to everyone as a local Ethereum environment for rapid DAPP development.

       Note that although the account in Ganache has mnemonics and private keys, (personal suggestion) it is best not to use it in a formal environment, and do not store digital assets in it.

       Everyone is welcome to point out errors or suggest improvements.

       Official website: https://www.trufflesuite.com/

Guess you like

Origin blog.csdn.net/weixin_39430411/article/details/104248037