1. What is truffle
1 Overview
Truffle is a world-class Ethereum development framework, which makes Ethereum development simple and efficient. It has the following characteristics:
- Built-in smart contract compilation, linking, development and binary management.
- Automated contract testing for rapid development
- Scripting, scalable deployment and migration framework.
- Network management for deployment to any number of public and private networks
- Based on EthPM and NPM, and use ERC190 standard for package management
- Provides an interactive console for contract communication
- Provides a configurable build pipeline for tight integration
- Execute external running scripts in the Truffle environment
2. Official website
The official website is unstable and often cannot be opened
3. Official website localization
Clone the code locally, compile it, and access it locally (perfect!)
2. How to install truffle
npm install -g truffle
Run the following command to test the installation:
truffle version
3. Create an empty project
3.1 Preparations
mkdir truffleTest
cd truffleTest
3.2 Executing commands
truffle init
Execution effect:
the following files will be generated in the current directory:
- contracts: store contract files
- migrations: store deployment related files
- test: used for unit testing
- truffle.js: configuration file, which tells truffle which network to deploy to (test network, main network, private network, etc.), non-windows platform use
- truffle-config.js: same as truffle.js, only for windows platform
3.3 Add contract code
Under the contracts folder, create a file SimpleStorage.sol, and paste the following code:
pragma solidity ^0.8.14;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
3.4 truffle compile contract
Execute the following command in our project:
truffle compile
At this time, the build directory will be generated, and the results in it are as follows:
compile will compile our Solidity code into bytecode (code that the Ethereum Virtual Machine (EVM) can understand), and the two files in the build folder The file is generated according to the contract. Each .sol file will compile a .json file. The json description file contains the contract's abi, bytecode and other related information for subsequent deployment.
3.5 truffle deployment contract
We generally deploy in the following three environments:
- ganache graphical environment/ganache-cli command line environment
- Real environment (test/mainnet)
3.7 Add deployment script
Open the "migrations" folder and create a new file called "2_deploy_contracts.js". Migrations are just scripts that help us deploy contracts to the blockchain. Paste the code below into it, and save.
```go
const SimpleStorage = artifacts.require("./SimpleStorage.sol");
module.exports = function (deployer) {
deployer.deploy(SimpleStorage);
};
Line 1 is used to import the "SimpleStorage.sol" file (exported from the "contracts" folder), and line 4 deploys it to the blockchain.
4. Truffle deployment contract
4.1 Deployed in Ganache
Install command line ganache-cli
sudo npm install -g ganache-cli
Modify truffle-config.js as follows:
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
ganacheNet: {
//ganacheNet是可以⾃定义的名字,链接到ganache客户端
host: "127.0.0.1",
port: 8545,
network_id: "*" // match any network
}
}
}
After starting ganache, execute the following command
truffle migrate --network ganacheNet
This ganacheNet is what we added manually in the truffle.js configuration file
4.2 Deploy in truffle development mode
Execute the following command in the project:
truffle develop
Start the built-in development environment, the effect is as follows:
After starting, it will enter the interactive interface, start the local service by default, occupy the port number 9545 , and create 10 virtual accounts at the same time, each account contains 100 eth
-compile commands
by default in the terminal Execute compile
the command directly in the command to complete the compilation.
Result: The build directory will be generated, same as above.
- migrate command
Execute migrate
the command directly in the terminal to complete the deployment. (It does not need to deploy the network here, it is a virtual network itself, but we can't see it)
When we execute migrate, this command will update the SimpleStorage.json file and will be deployed on this specified network The contract address in and the corresponding transaction hash are saved in. Then we can call our contract through the information in this json file. (described later)
5. Interact with the contract
The truffle console comes with a web3 instance, and a contract instance has been instantiated in the console, which can be used directly. This instantiated contract is not the mode we used before, but an instance wrapped with a library called truffle-contract.
- In this development environment, there is a built -
in web3 instance, we can check the version of web3:
we use the console to interact with the contract - When using the truffle framework to obtain a contract instance
, we will use the truffle library: truffle-contract, which encapsulates the native web3.js to achieve more convenient contract calls. - Call the set function
- call get function
Six, test test
The contract must start with Test, and the function name must start with test
Code:
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/SimpleStorage.sol";
//合约名字要Test开头
contract TestMetaCoin {
//测试函数要test开头
function testSet() public{
SimpleStorage ss = SimpleStorage(DeployedAddresses.SimpleStorage());
ss.set(100);
uint res=ss.get();
Assert.equal(res,100,"res shoulid be 1000");
}
}
implementtruffle test