4. Use Truffle to Deploy Smart Contract

Truffle is available for conflux eSpace, please note!
Truffle, a world class development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM), aiming to make life as a developer easier.
Starting from ChainIDE 2.1 version, the Terminal function is supported. You can use the test compilation and deployment functions of the truffle framework. At the same time, it provides ganache-cli as a virtual environment to start.
The purpose of this tutorial is to teach how to use truffle in chainIDE. As for a detailed tutorial, please read truffle documentation

Open terminal

Click "Logger" in the lower right corner, then click the "Terminal" in the middle, nice, you can see Terminal now!
8. Open the Terminal

Creating a project

To use most Truffle commands, you need to run them against an existing Truffle project. So the first step is to create a Truffle project.
You can create a bare project template, but for those just getting started, you can use Truffle Boxes, which are example applications and project templates. We'll use the MetaCoin box, which creates a token that can be transferred between accounts:
  1. 1.
    Create a new directory for your Truffle project:
    mkdir MetaCoin
    cd MetaCoin
  2. 2.
    Download ("unbox") the MetaCoin box:
    truffle unbox metacoin
    Note: You can use the truffle unbox <box-name> command to download any of the other Truffle Boxes.​​
    Note: To create a bare Truffle project with no smart contracts included, use truffle init.​
Once this operation is completed, you'll now have a project structure with the following items:
  • contracts/: Directory for Solidity contracts
  • migrations/: Directory for scriptable deployment files
  • test/: Directory for test files for testing your application and contracts
  • truffle-config.js: Truffle configuration file

Testing

  1. 1.
    On a terminal, run the Solidity test:
    truffle test ./test/TestMetaCoin.sol
    You will see the following output
    TestMetacoin
    √ testInitialBalanceUsingDeployedContract (71ms)
    √ testInitialBalanceWithNewMetaCoin (59ms)
    2 passing (794ms)
    Note: If you're on Windows and encountering problems running this command, please see the documentation on resolving naming conflicts on Windows.​
    These two tests were run against the contract, with descriptions displayed of what the tests are supposed to do.
  2. 2.
    Run the JavaScript test:
    truffle test ./test/metacoin.js
    You will 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)

Compiling

  1. 1.
    Compile the smart contracts:
    truffle compile
    You will see the following output:
    Compiling .\contracts\ConvertLib.sol...
    Compiling .\contracts\MetaCoin.sol...
    Compiling .\contracts\Migrations.sol...
    Writing artifacts to .\build\contracts

Migrating with Truffle Develop

Note: To use Ganache, please skip to the next section.​
To deploy our smart contracts, we're going to need to connect to a blockchain. Truffle has a built-in personal blockchain that can be used for testing. This blockchain is local to your system and does not interact with the main Ethereum network.
You can create this blockchain and interact with it using Truffle Develop.
  1. 1.
    Run Truffle Develop:
    truffle develop
    This shows ten accounts (and their private keys) that can be used when interacting with the blockchain.
  2. 2.
    On the Truffle Develop prompt, Truffle commands can be run by omitting the truffle prefix. For example, to run truffle compile on the prompt, type compile. The command to deploy your compiled contracts to the blockchain is truffle migrate, so at the prompt, type:
    migrate
    You will see the following output:
    This shows the transaction IDs and addresses of your deployed contracts. It also includes a cost summary and real-time status updates.​
    Note: Your transaction hashes, contract addresses, and accounts will be different from the above.​
Note: To see how to interact with the contract, please skip to the next section.​