Truffle
Introduction to Truffle
Truffle is a world-class blockchain development environment, testing framework, and asset pipeline using the Ethereum Virtual Machine (EVM), designed to make developers more productive. The Truffle tool suite (truffle suite) is divided into four parts: Truffle, Ganache, dirzzle, and Truffle for Vscode. Among them, Ganache provides the "One click blockchain" function, as long as you click to run Ganache, you can deploy the blockchain network locally for developers to test, develop and deploy dApps. Truffle for Vscode is an extension of Visual Studio Code, which aims to move the functions of Truffle to the most popular IDE environment.
Features of Truffle
Truffle is the most comprehensive smart contract development suite with the following features:
- Built-in smart contract compilation, linking, deployment and binary management.
- Advanced debugging with breakpoints, variable analysis, and step functionality.
- Truffle is written based on JavaScript and can be debugged using console.log in smart contracts
- Deploy and trade with MetaMask and Truffle Dashboard , protecting your mnemonic.
- An external script runner that executes scripts within the Truffle environment.
- Automated contract testing for rapid development.
- Use NPM for package management, using the ERC190 standard
- Scriptable, scalable deployment and migration architecture
- Interactive console available for contract communication
Truffle for Vscode plugin installation
To use truflle in vscode, you need to install the official truffle plug-in and configure related dependencies.
Prerequisites: 1. Install the Truffle for VSCode plugin 2. Install Truffle for VSCode dependencies
The second step is to install Truffle dependencies
To use Truffle, you need to install node.js, npm, git, truffle, and ganache. The versions of node.js and npm need to be within the specified range and not too high, otherwise the Truffle plug-in cannot be used. The node and npm versions I installed are 16.12.0 and 8.19.4 respectively.
Downloading node.js and switching npm versions don't need me to say much, let's start using npm to download truffle.
npm install -g truffle
Check the version of truffle to make sure it is installed correctly
truffle version
Ganache is a tool in the Truffle toolkit for building personal development blockchains. Go to the download page and select the Windows version to download.
Clicking Run Ganache creates the Ethereum blockchain locally, which can be used to run tests, execute commands and check status, while controlling how the chain operates.
Write a smart contract
First, start an empty Truffle project in Vscode.
And connect to the local Ganache service.
Write the contract code Hellworld.sol in the contracts directory (csdn's md editor does not support solidity, -__-)
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.22 <0.9.0;
contract Helloworld {
function hello() public pure returns (string memory) {
return "hello world";
}![在这里插入图片描述](https://img-blog.csdnimg.cn/539af20e4bcb49f6b7f82adaf7ce09c3.png#pic_center)
}
Write the migration file 1_hello_world.js in the migrations directory (note: the migration file name must start with an Arabic numeral, which is related to the order of execution)
const Helloworld = artifacts.require("Helloworld");
module.exports = function(deployer) {
deployer.deploy(Helloworld);
}
Compile the contract first. If there is a problem with the compilation, check the code of the contract. Generally, there will be a compilation error message. After the compilation is successful, right-click the source code to deploy the contract.