3. Use Truffle to deploy a smart conract
Compile, deploy, interactive query and so on through truffle. For further study, please read truffle official documentation: https://trufflesuite.com/docs/truffle/

1. Use Truffle to deploy the smart contract

Truffle uses the Ethereum Virtual Machine (EVM) to provide a world-class development environment, testing framework, and asset management for the blockchain, aiming to make development easier for developers. Starting from ChainIDE 2.1 version, the Terminal function is supported, and you can use the background to perform test compilation and deployment functions of the truffle framework. The purpose of this tutorial is to teach how to use truffle in chainIDE, and for specific truffle usage, please refer to its related documentation.

Open the Terminal from Logger

Click "output" in the lower right corner, then click "command line" in the middle, and then click "npm-truffle" to switch to the command-line interface.

Create project directory

Most Truffle commands are run in the Truffle project directory. So the first step is to create a Truffle project. You can create an empty project template, but for students who are new to Truffle, Truffle Boxes is recommended, which provides sample application codes and project templates. We will use the MetaCoin box as a case. It creates a Token that can be transferred between accounts. Developers can modify the relevant files in the case for development, which is much more convenient.
  1. 1.
    Create a new directory for the Truffle project:
mkdir MetaCoin
cd MetaCoin
2. Download ("unbox") MetaCoin box:
truffle unbox metacoin
You can also use the truffle unbox <box-name> command to download other boxes
If you want to create an empty projet without a contract, you can use truffle init
After the operation is completed, there is such a project structure:
  • contract/: Solidity contract catalog
  • migrations/: Deployment script file directory
  • test/: Test script directory, refer to how to test the application?
  • truffle-config.js: Truffle configuration file

Test Case

  1. 1.
    Open the console terminal and run the Solidity test case:
truffle test ./test/TestMetaCoin.sol
We can see the following output:
TestMetacoin
√ testInitialBalanceUsingDeployedContract (71ms)
√ testInitialBalanceWithNewMetaCoin (59ms)
2 passing (794ms)
When running test cases. The desired behavior will be output to the console.
  1. 1.
    Run JavaScript test cases
truffle test ./test/metacoin.js
We can see the following output:
Contract: MetaCoin
√ should put 10000 MetaCoin in the first account
√ should call a function that depends on a linked library (40ms)
√ should send coin correctly (129ms)
3 passing (255ms)

Compile the contract

Compile the smart contract:
truffle compile
We can see the following output:
Compiling .\contracts\ConvertLib.sol...
Compiling .\contracts\MetaCoin.sol...
Compiling .\contracts\Migrations.sol...
Writing artifacts to .\build\contracts

Use Truffle Develop to deploy the smart contract

If you use Ganache, you can skip direclty to the next part.
In order to deploy our contract, we need to connect to the blockchain network. Truffle provides a built-in personal simulation blockchain, which can help us to test. Note that this blockchain is the mainland in our local system, and it is not connected to the Ethereum network.
We can use Truffle Develop to create a blockchain and interact with it.
  1. 1.
    Run Truffle Develop:
truffle develop
The console will display 10 accounts and their private keys to you. These accounts can be used to interact with the blockchain.
  1. 1.
    At the truffle(develop)> prompt (because an interactive console is provided), Truffle commands can be executed without the prefix truffle. For example, you can directly enter compile to execute truffle compile, and directly enter migrate to deploy the compiled smart contract to the blockchain (equivalent to truffle migrate):
migrate
The console will display the ID number of the transaction and the address of the deployed contract. And the cost of the transaction and some related status.
Copy link
On this page
1. Use Truffle to deploy the smart contract
Open the Terminal from Logger
Create project directory
Test Case
Compile the contract
Use Truffle Develop to deploy the smart contract