1. Initialize the project
mkdir helloworld cd helloworld truffle init
Create a new folder, use truffle init to initialize, sometimes it will not connect, just try a few more times.
Download the folder to the local using ftp, the file directory is as follows:
The file directory is explained as follows:
- contract /-Truffle default contract file storage address.
- migrations /-store release script files
- test /-test file for testing applications and contracts
- truffle-config.js-Truffle configuration file
Second, write smart contracts
2.1. Writing smart contracts
Create a new contract file Greeter.sol under contract
pragma solidity >=0.4.25 <0.7.0; contract Greeter { address creator; string greeting; constructor(string memory _greeting) public{ creator = msg.sender; greeting = _greeting; } function greet() public view returns(string memory) { return greeting; } function setGreeting(string memory _newgreeting) public{ greeting = _newgreeting; } }
2.2. New release script
Create a new file in the ./migrations/ directory: 2_deploy_contracts.js and add the release code.
var Greeter = artifacts.require("./Greeter.sol"); module.exports = function(deployer) { deployer.deploy(Greeter,"Hello, World!"); };
2.3. Modify the configuration file truffle-config.js
module.exports = { networks: { development: { host: "127.0.0.1", // Localhost (default: none) port: 8545, // Standard Ethereum port (default: none) network_id: "*", // Any network (default: none) } }, // Set default mocha options here, use special reporters etc. mocha: { // timeout: 100000 }, // Configure your compilers compilers: { solc: { // version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version) // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) // settings: { // See the solidity docs for advice about optimization and evmVersion // optimizer: { // enabled: false, // runs: 200 // }, // evmVersion: "byzantium" // } } } }
Third, the deployment contract
truffle compile ganache-cli truffle migarte
Four, test contract
truffle provides a simpler way to interact with your prepared contracts through an interactive console.
truffle console
A basic interactive console can be connected to any EVM client. If you already have your own local environment for EVM such as ganache or geth, then you can use the truffle console to interact, so if you already have an off-the-shelf development EVM shared by the team, then use this right.
truffle develop
An interactive console will automatically generate a blockchain environment for development when it is started (in fact, I think it and ganache are an underlying implementation mechanism, and both generate 10 accounts by default). If you do not have your own EVM environment, it is very convenient to use truffle develop directly.
Enter the basic interactive console
truffle console
Enter the Greeter smart contract command, display and print out a json structure, showing its various attributes.
Call contract
Greeter.deployed().then((instance) => { greeter = instance } ) greeter.greet()
Reference: https://www.jianshu.com/p/2e2b3b12eb0e