3. Conflux Truffle Usage
Conflux-Truffle is available for conflux Core, please note! This page discusses how to set up Conflux-rust local node start-up, installation, and use of Conflux-truffle

1. Conflux-Truffle Installation

Open the command line

Click "Output" in the lower right corner, and then click "Command Line" in the middle to switch to the command-line interface. Click "+" to select the conflux-truffle mirror.
Check whether the cfxtruffle version is the latest:
cfxtruffle version
If the default version is not the latest, you should update it first using the following command.
$ npm install -g conflux-truffle
After the installation is successful, you can use the command cfxtruffle for smart contract development

2. Run the local node

For smart contract development, a local node is a must. Ganache is provided in the truffle suite, which is a local Ethereum node, which is very convenient for development. Conflux currently provides the Docker mirror confluxchain/conflux-rust of conflux-rust, which can also be very convenient Start the local node. However, in view of the risks of the web client, a new method is used to start the local node in ChainIDE.
Create a new Conflux-rust image
Enter the following commnad to start the local node:
cd
cd run
./start.sh
Note: When the image is started for the first time, 10 accounts will be automatically created, and each account will be assigned 1000 CFX, which can be used for testing. The image runs in dev mode by default.

3. Local node port mapping

Since 127.0.0.1 cannot be used directly in chainIDE, we need port mapping to achieve the same effect.
Open port as shown in the figure below.
Select the conflux-rust image, the port number is 12537, and add it.
The generated local link is very important and will be used in network configuration later.

4. Use cfxtruffle to deploy the smart contract

Conflux-Truffle is the development environment, testing framework, and asset pipeline of Conflux, designed to make development easier for Conflux 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 command line

Switch to the previously opened conflux-truffle mirror.

3. Create Project Engineering

Truffle provides many project templates called boxes, you can use the unbox command to download templates for rapid development (sometimes you need to overturn the wall)
$ cfxtruffle unbox metacoin
The contents of the created project directory are as follows:
  • build Store the compiled files of the contract(json)
  • contracts Contract code directory
  • migrations Contract migration script directory(js script)
  • test Test file
  • truffle-config.js Configuration file

Configure truffle-config.js for local deployment

module.exports = {
networks: {
development: {
host: "************", // ****The local link generated for the previous port mapping and remove the "https//”
port: 80, // Standard Conflux port (default: none)
network_id: "*", // Any network (default: none)
},
},
}
Replace ********** with the rust local node port mapping link generated by yourself before, and remove the previous https://

Compile the contract

$ cfxtruffle compile
The compiled content will be stored in the build directory.

Contract Deployment

$ cfxtruffle deploy # or cfxtruffle migrate
Deploy and migrate are essentially a command. Truffle uses migration scripts to manage the deployment and migration of contracts, specifically
  1. 1.
    Each script in the migration folder corresponds to a deployment task. To add a new contract, you need to add the corresponding migration script
  2. 2.
    All truffle projects will come with a Migration contract to store the location of the last deployment of the current project: last_completed_migration, setCompleted()
  3. 3.
    When cfxtruffle deploy is executed, the last deployment location will be queried and executed from the next task. After the execution is completed, the current deployment sequence number will be updated.
In addition, cfxtruffle deploy has several parameters (--reset, --from, --to) to control the execution rules of migration. For details, please refer to cfxtruffle help

Contract Interaction

cfxtruffle also provides a command-line interactive environment in which the status of the contract can be directly obtained or updated. In the console environment, not only the js class corresponding to the contract is integrated, but also cfxcfxutil Objects can be used directly, which is very convenient (refer to js-conflux-sdk for specific usage)
$ cfxtruffle console # Execute in the project directory, open interactive mode
# Initial contract instance
cfxtruffle(develop)> let instance = await MetaCoin.deployed()
cfxtruffle(develop)> instance
# Get balance
cfxtruffle(develop)> let balance = await instance.getBalance(accounts[0])
cfxtruffle(develop)> balance.toNumber()
# transfer
cfxtruffle(develop)> let result = await instance.sendCoin(accounts[1], 10, {from: accounts[0]})
cfxtruffle(develop)> result

Contract Testing

cfxtruffle integrates Mocha and chai test frameworks by default and can be run through the test subcommand.
Sample unit test code:
const MetaCoin = artifacts.require("MetaCoin");
contract('MetaCoin', (accounts) => {
it('should put 10000 MetaCoin in the first account', async () => {
const metaCoinInstance = await MetaCoin.deployed();
const balance = await metaCoinInstance.getBalance.call(accounts[0]);
assert.equal(balance.valueOf(), 10000, "10000 wasn't in the first account");
});
}
Perform the test:
$ cfxtruffle test

4. Deploy to a remote node

The contract is developed locally. After the test is completed, you can try to deploy it to the test network or publish the contract to the main network. cfxtruffle also supports remote deployment. First, you need to add a new network configuration such as testnet in the project configuration file, and set the host and port in it. Then you need to set the private Keys field, which is an array of private keys (remote deployment can only be signed locally).
Added in truffle-config.js:
testnet: {
host: "wallet-testnet-jsonrpc.conflux-chain.org",
port: 12537,
network_id: "*",
// Note: The private key obtained from the portal needs to be prefixed with 0x. PrivateKeys can also specify a single key. If the private key is configured, please be careful not to upload the code to the public code repository.
privateKeys: ['your-private-key']
},
Test environment of Conflux portal The deposit button provides a CFX faucet, and you can apply for some test environment tokens for contract deployment. Then you can deploy the contract to the specified network by specifying --network when executing the deploy command:
cfxtruffle deploy --network testnet
In this way, the account corresponding to the private key configured here will be used when deploying the contract and interacting with the contract.

5. Learning Material: